Migrating from Django template to Jinja2

Jinja2 is great. Far better than the django template I think. But I’m not here to talk about that. My guess is you already decided to use Jinja2. So let’s talk about the changes you need.

1. You can use Jingo to set it up easily.

pip install jingo

Create a file e.g. ‘jingo_settings.py’ & paste the following code:


# JINGO_INCLUDE_PATTERN = r'\.jinja2'  # use any regular expression here

JINGO_EXCLUDE_APPS = ('debug_toolbar',)

    'autoescape': False,
    'extensions': ['jinja2.ext.with_',]

Now import everything at the end of your ‘settings.py’. e.g.

from jingo_settings import *

You might need extensions other than ‘jinja2.ext.with_’. Feel free to add new ones to the list.

2. ‘url’ tag doesn’t work like a tag anymore. that is, they don’t start with a {% in jinja2. You are going to change all url tags to functions like:

{{ url("auth-login") }}

3. ‘csrf_token’ is also not a tag in jinja2. Use the following instead of {% csrf_token %} in jinja2

<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">

4. If you are printing form fields in the template remember that ‘value’ & ‘as_hidden’ are function. So use these like:

{{ form.name.as_hidden() }}


{{ form.name.value() }}

Leave a Reply