I’m learning how to code using python’s flask but I can’t seem to sucessfully run my server. For some reason, I keep getting Template not found but I do have my index.html in my templates folder just like flask wants me to. I’ve attached an image showing the folder. folders and error
I have tried using the "app = Flask("Web Translator", template_folder=’new folder name’)" and changing the folder name and replacing the template_folder="new folder name" but it does not work. When I try to run my server it shows this:enter image description here
2
Answers
This is how you should create the Flask Object
The
__name__
variable is a special variable that represents the name of the current module. When you run a Python script directly, the__name__
variable is set to__main__
.In the context of
Flask
, the__name__
variable is used to determine the root path of the application. It helps Flask locate the root directory of the project and find other files and folders relative to that path.Now after looking at your code, the page you want to render is in
new folder nametemplateindex.html
then your code shall work fine.
In your code, you have
return render_template('templates/index.html')
. Flask will look for a folder called ‘templates’ within the default template folder.You should simply have it as
return render_template('index.html')
Also, conventionally, you do
app = Flask(__name__)
. See this for an explanation on why