Flask and Jinja2 Templating – Basic Tutorial

  1. 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.
  2. 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.
  3. Create your first template file within this folder, say “layout.html”:


    {{ a_random_string }} {{ a_random_list[3] }}
  4. In your main application file, make sure you have imported render_template:
    from flask import render_template
  5. and here is an example of a view that uses the template:
    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)

That’s it!

Leave a Reply

Your email address will not be published. Required fields are marked *

This blog is kept spam free by WP-SpamFree.

Get Adobe Flash player