Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask Template Not found [duplicate]

Implementing a simple static site from flask, but the browser says template not found, the shell returned 404

jinja2.exceptions.TemplateNotFound  TemplateNotFound: template.html 

The main python code:

from flask import Flask, render_template app = Flask(__name__)   @app.route("/") def template_test():     return render_template('template.html', my_string="Wheeeee!", my_list=[0,1,2,3,4,5])  if __name__ == '__main__':     app.run(debug=True) 

I have the following file structure:

flask_new_practice |--template/     |--template.html |--run.py 
like image 601
saviour123 Avatar asked Oct 28 '15 16:10

saviour123


People also ask

How do I fix flask template not found?

To fix Python Flask raising TemplateNotFound error even though template file exists, we can set the template_folder to the folder with the templates in our project. to set the template_folder argument to the path string relative to our project folder.

Where does Flask look for templates?

Flask looks for templates in the templates directory, which is called templates , so the name is important. Make sure you're inside the flask_app directory and run the following command to create the templates directory: mkdir templates.

What does render_ template do?

render_template is a Flask function from the flask. templating package. render_template is used to generate output from a template file based on the Jinja2 engine that is found in the application's templates folder. Note that render_template is typically imported directly from the flask package instead of from flask.

How do I fix Jinja2 exceptions TemplateNotFound index html?

To resolve the issue, simply create a folder name it “Templates”. Then move “index. html” into this newly created folder.


1 Answers

By default, Flask looks in the templates folder in the root level of your app.

http://flask.pocoo.org/docs/0.10/api/

template_folder – the folder that contains the templates that should be used by the application. Defaults to 'templates' folder in the root path of the application.

So you have some options,

  1. rename template to templates
  2. supply a template_folder param to have your template folder recognised by the flask app:

    app = Flask(__name__, template_folder='template') 
like image 101
Jeffrey Godwyll Avatar answered Sep 23 '22 22:09

Jeffrey Godwyll