- Firstly, make sure Jinja 2 is installed:
pip install Jinja2
It will probably say “Requirement already satisfied” as by default I think it comes packaged in with Flask.
- Create a folder in your root Flask application directory named
templates. Flask will automatically try to find templates in this folder so don’t explicitly specify it in the path below.
- Create your first template file within this folder, say “layout.html”:
- In your main application file, make sure you have imported render_template:
from flask import render_template
- and here is an example of a view that uses the template:
@app.route("/template_test") def template_test(): rand_list= [0, 1, 2, 3, 4, 5] return render_template('layout.html', a_random_string="Heey, what's up!", a_random_list=rand_list)