Django Template/Jinja2 Quick Reference

Templating is my favorite part of Python web dev, it's a fast evolving technology, so there's a lot of trial and error.

Having the pleasure of working with both Jinja2 and Django templating extensively, I wrote this to help me not make the same mistakes twice.

While Jinja2 is technically compatible with Django, I only found solutions for it in Python 2.7, which is on the way out. I tend to hack at Django is little as possible, as it is huge, complex and always changing.

That said, Jinja2 is far superior technically, in fact there is allegedly a security vulnerability in Jinja 2.10.


PyCharm supports template debugging and code analysis. I like to use the jinja2 extension to make easier for the IDE to recognize. I've also seen j2 used by some.

I always use html extensions for my templates in Django templates, and PyCharm is smart enough to recognize them. I just think it's better organizationally to give templates a different extension.

Static files

One annoying thing about Django templating is that you have to

 for every template, even if it's extending a base template.


As far as I know there is not a way to implement if=false, I found a workaround:

{% if logged_in %}{% else %}
. Note that you need to explicitly use if for this to work. Django everyone!

Even though in Django you can use

{% logged_in %}
you must close with and
{% endblock %}
rather than
{% endif %}

Technical Support