I am working on a python project using the flask module. It's not the first time i've used it but i am still very much a novice. While i was setting up a very simple file this error came up: UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte This is my app.py file:
from flask import Flask,render_template
app = Flask(__name__, template_folder='templates')
@app.route("/")
def index():
return render_template("index.html")
if __name__ == '__main__':
app.run(host="0.0.0.0", debug=True)
and this is my index.html file:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Flask app</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
The problem only occurs when i try to render the template as if i take out the render template and substitute it with somthing like return "Hello world"
i don't have any problems and by going to the site i see Hello world.
What is the cause of the error? Python's version is 3.10 and i am on a linux pc
I've tried to redo this code on my windows pc with python 3.9.13 i have no problem, while by using my linux computer with python 3.10 i keep encountering it.
I think it might be happening because of the encoding you use while editing the HTML. Try to run this code and run the app.py afterwards:
f1 = open("templates/index.html","r").read()
open("templates/index.html","wb").write(f1.encode("utf-8"))
I hope it works!