I've got a couple small (500 or 600 lines of template code) Django sites, and I'd like to migrate them to using Jinja2… But I'd like to get some idea of how much work it will be. So, in general, about how much work is it to migrate a small Django site to Jinja2? And is it “worth it”?
-
3Whether something is "worth it" depends on your criteria. What are you trying to achieve? What does Jinja2 allow you to do that Django templates don't? – Dominic Rodger Dec 02 '10 at 17:00
-
3Mostly I'd like simpler debugging (damnit, Django, why do you ignore undefined variables?!), simpler template tags, the ability to have a little bit more code in templates (eg, Python syntax for basic list and math operations), and generally it just seems like a “better designed” language. – David Wolever Dec 02 '10 at 17:16
-
5Not enough to be an answer, but for me it was worth it. I was feeling seriously constrained by Django's restriction on logic in templates (how much restriction there should be is a holy war, but in *my opinion* it was too much -- and judging from your comments, you think so too). Switching to Jinja2 let me keep mostly the same syntax (with a few clunky things cleaned up), which I like, and because of the additional features it felt like an upgrade. I also like the idea of the template engine being completely independent of whatever framework I happen to be using. – Cameron Dec 05 '10 at 18:10
-
2(damnit, Django, why do you ignore undefined variables?!) - tip: pycharm 1.5 supports template debugging, that means that you can attach breakpoints in the templates. – panchicore Sep 22 '11 at 14:45
-
I used [django-jinja](https://django-jinja.readthedocs.org/en/latest/) and it works very well. To have a look at what it would be to change from django template to jinja, have a look at the [differences page](https://django-jinja.readthedocs.org/en/latest/differences.html). Unfortunately, I can't set any breakpoints in `jinja2` files with PyCharm even after I set the `Jinja2` as the default `Python Template Language` in PyCharm Settings as of `3.1.1`. I still think it was worth it. – GabLeRoux May 29 '14 at 17:23
-
Edit from last comment (past 5 minutes). Documentation on rtfd looks outdated, those on github seem better [django-jinja](http://niwibe.github.io/django-jinja/) and [django-jinja differences](http://niwibe.github.io/django-jinja/#_differences) – GabLeRoux May 29 '14 at 17:36
4 Answers
While it's just my own experience, I found converting from Django to Jinja2 to be worthwhile for the following reasons:
- The design and implementation of Jinja2 seemed more intuitive to me, both as a software developer and template designer;
- Jinja2 is more extensible (at least in the ways I've sought to extend my template engine);
- Jinja2 is more flexible in terms of permitting logic-code to be run (but it gives you enough rope to hang yourself with);
- Jinja2 is regarded as significantly faster (though I haven't done any benchmarks, this is always subject to debate depending on the tests used, and in any event largely irrelevant in the total wait time for a query that has to do DB lookups);
Jinja2 gives significantly more helpful error output than Django (i.e. traces to the line number in the template where the error occurred).Edit: According to Dor's comment, Django gives helpful error messages that point to the line and context of a problem, much like Jinja2.
If you haven't had any trouble with Django's template engine, Jinja2's should feel relatively intuitive, if perhaps a bit more polished (or it did to me, at any rate). As well, I found the Coffin
project well written and reasonably helpful when converting from Django to Jinja2 – both for its use, and as an example of how to extend Jinja2.
All that being said, Django's template engine is solid and quite capable for most tasks. I believe it's being improved in the next revision of Django, and there is quite a lot of effort to add to its capabilities by quite a number of dedicated developers. As a result there are no worries of it becoming unsupported in the near to medium-term future.
Again, that's just my experience, for what it's worth – I hope that's helpful.

- 81,008
- 74
- 230
- 343
-
Django does show the erroneous line (and the surrounding lines) in the template code. – Dor Apr 16 '12 at 17:00
-
@Dor: That may be the case now, but it wasn't at the time I wrote the answer. Will fix. :) – Brian M. Hunt Apr 16 '12 at 17:21
-
1
-
Just tested with Django from SVN, though I'm sure 1.4 has this feature too. Changed an endfor tag to endfr, got this exception: `Invalid block tag: 'endfr', expected 'endfor'` with this line further down: `In template C:\...\templates\browser\item_list.html, error at line 187`. Also shown was the template code around the problematic line. – Dor Apr 17 '12 at 13:59
-
With respect to #4, I've seen people say this as a truism but it's not always the case. I have a current Django 1.6 site for which the database lookups are an order of magnitude smaller than the template rendering time. Use something like the Django Debug Toolbar and make your own decision as to whether a Jinja migration makes sense. – Scott A May 24 '14 at 13:15
-
The question is asking "how much work is it to switch" and "is it worth it". This answer does not address the question. – mehmet Aug 18 '16 at 01:40
There's also django-jinja. https://github.com/niwibe/django-jinja
New and nice project. http://niwinz.github.io/django-jinja/latest/
It claims to be a simple and nonobstructive jinja2 integration with Django.

- 172
- 1
- 13

- 1,044
- 12
- 23
From what you have said, it's may not be worth the trouble to migrate to Jinja2. There are filters in Django Templates which could help you do any math operations.
Regarding list operations, what exactly are you talking about? If you want some particular list operation to be supported in Template, than write a custom filter.
There are also some existing 3rd party math filters for Django.
If you think about it, it's by design that Django templates does not have too much of "programming constructs" in them. HTML templates should be such...

- 71,928
- 54
- 216
- 264
-
8I agree that templates shouldn't have “too much” programming… I just feel like the features of Jinja 2 are a bit more pragmatic. Same deal with, eg, list operations: I could write a templatetag or something, but I feel like it would be overall more pragmatic if I could use, eg, `{% for x in ['a', 'b'] %}...{% endfor %}`. – David Wolever Dec 05 '10 at 01:24
Two projects which are trying to integrate Jinja in Django with simple integration steps and nicely hooks with Django -
I am integrating them right away to experiment if they work too with Django contrib apps like Django Admin. But I feel, it will be available for project apps and Django default template system will be used in internal apps like Admin.

- 1,382
- 16
- 22