Django: Switching to Jinja2?

David Wolever picture David Wolever · Dec 2, 2010 · Viewed 27.3k times · Source

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”?

Answer

Brian M. Hunt picture Brian M. Hunt · Dec 4, 2010

While it's just my own experience, I found converting from Django to Jinja2 to be worthwhile for the following reasons:

  1. The design and implementation of Jinja2 seemed more intuitive to me, both as a software developer and template designer;
  2. Jinja2 is more extensible (at least in the ways I've sought to extend my template engine);
  3. Jinja2 is more flexible in terms of permitting logic-code to be run (but it gives you enough rope to hang yourself with);
  4. 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);
  5. 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.