Janne Pulkkinen
aka Matoking

If you've worked with Django, one of the most popular Python-based web frameworks, then you are using or have used the framework's own template engine DjangoTemplates. While most of the Django libraries and projects assume you use the default template engine, Django also has official support for a different template engine called Jinja2. And although the default template engine is completely fine most of the time, there are some advantages to using Jinja2 instead of DjangoTemplates, which I will explain in the course of this blog post.

What is Jinja2

Jinja2, like DjangoTemplates, is a templating language and a template engine. It is also based on the Django's own templating language, which will no doubt be useful if you're going to port templates from Django to Jinja2; most of the time you only need to make a few modifications to the template and sometimes none at all.

For example, the following template from Jinja2's documentation works identically in both templating languages:

Why use Jinja2 (instead of DjangoTemplates)

As I stated earlier, Django's own templating language is in most cases adequate. However, Jinja2 has some advantages when compared to DjangoTemplates, which may be worth considering when starting a new project in Django.

Jinja2 is a general-purpose templating language

​While DjangoTemplates is used solely within Django, Jinja2 is a templating language that is also used with other web frameworks such as Flask. You can also use it with other Python applications, regardless of whether you're using a web framework or not (the output doesn't have to be HTML, either), making its potential use cases more diverse than Django's.

Jinja2 is more flexible

​With Django, you have a limited subset of expressions you can use in templates to encourage separation of logic from views when it is not necessary. The most notable difference Jinja2 makes is that it allows more diverse Python expressions. For example, you can call an object's methods as you wish in Jinja2, whereas in Django you would need the aid of a template tag or filter if you need to do something more expressive, like passing multiple arguments to the method. For example:

You can also use the methods provided by different Python modules as long as you remember to import them (more on that later). This also means that instead of coding a template tag that parses your parameters and returns a string, you can just import a plain old Python method and use it as-is.

Of course, just because you can use more logic in Jinja2 templates doesn't mean it's always necessary. Still, sometimes this may save you from writing a few template tags when you can import the relevant Python methods directly.

Jinja2 is (possibly) faster

​Since Jinja2 utilizes JIT compilation and is by default more lightweight than DjangoTemplates, it's likely you could achieve a small performance improvement by porting your templates to Jinja2. This can depend on the specific view that is ported. In my tests, porting the frequently viewed paste view template in my pastebin application allowed around 45 requests per second with Jinja2 instead of 37 req/s with DjangoTemplates, amounting to a humble ~18% improvement.

However, since no two templates are the same, there's a chance you'll achieve very negligible or no improvements at all. And as is the case with most web applications, your main bottleneck is most likely I/O bound, making this point moot in most cases.

How to use Jinja2 in Django

Okay, so I've explained what Jinja2 is and a little how it compares to Django's own template engine, but I haven't told you how to enable it yet. Thankfully, the recent versions of Django come bundled in with built-in Jinja2 support (you still have to install Jinja2, but it's as easy as running pip install Jinja2). Once you're ready, open up your project's settings.py file and add the Jinja2 entry to the list of used template engines.

Simply said, the method creates a Jinja2 environment with all the settings and our app-related methods we need; in this example we've imported the reverse method from django.core.urlresolvers. Using it in the template is as easy as {{ url("front_page") }}. However, you've probably become accustomed to {% url "front_page" %} instead. There is a link to a library in the later section of this blog post that works as a zero-effort replacement that doesn't require you to change the syntax for every url template tag call.

Now, if you try starting the development server, you'll notice that the site will continue using your existing templates. This is normal, we simply told our project to give priority to the Jinja2 templates: if it finds Jinja2 templates, it will try loading them; if not, it wil fallback to our original Django templates. This also means you don't need to port all your templates from Django to Jinja2, but instead you can instead work your way up or simply port the templates where it makes sense to.

As for the location of the Jinja2 templates, whereas the normal Django template engine looks for templates in paths such as

So, simply create a new directory called jinja2 into the apps where you wish to use Jinja2 templates. Aside from that, the routes and the filenames should be identical.

Jinja2 in Django checklist and gotchas

Since the syntax between the two languages are almost identical, porting a template from DjangoTemplates to Jinja2 is not as much about rewriting the templates as it is about changing some of Django's own tags to use your own imported methods or Jinja2's own methods instead. With that in mind, here are some things you'll need to remember when porting your templates from DjangoTemplates to Jinja2.

jinja2-django-tagscontains Jinja2 implementations of the most used Django template tags (eg. csrf_token, static, url). After installing the library (very easy if you're using pip), using it is as easy as adding jdj_tags.extensions.DjangoCompat​ to the list of extensions used by Jinja2 in settings.py. No need to touch the original calls at all, they'll work out of the box!

When accessing the details of the current user in templates, use {{ request.user.username }} instead of {{ user.username }}. Although DjangoTemplates allows you to use both methods, with Jinja2 you'll have to explicitly access all of the properties through the request context.

Use brackets if you're calling a method. DjangoTemplates will determine whether a property is a primitive or a method and either print the primitive as-is or the method's result; in Jinja2 you'll have to be explicit. For example {{ request.user.get_username() }} will not work in Jinja2 without the brackets.

Use @contextfunction decorator if your global method needs to access the request context. For example, if you want to create a method that prints the currently logged-in user's name, you can write it as such:

In closing

Depending on the application you're creating, moving from the default template engine to Jinja2 can either be really easy and fast, or somewhat time-consuming. However, if you're about to start working on a new Django application, choosing Jinja2 instead of DjangoTemplates could save you a lot of time when it comes to customizing the template engine, as it allows custom methods to be added more easily and allows most of the Python syntax to be used inside the templates: a very powerful feature if used right. Truthfully, it is also a matter of personal choice and if DjangoTemplates is already working well for you, there's probably no need to toss it aside for a Jinja2-based implementation.