Flask Template Not found

PythonFlaskJinja2

Python Problem Overview


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

Python Solutions


Solution 1 - Python

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')
    

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionsaviour123View Question on Stackoverflow
Solution 1 - PythonJeffrey GodwyllView Answer on Stackoverflow