Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Any drawbacks or gotchas to using Jinja2 templates in Django?

Tags:

After reading the Jinja2 documentation, I'm interested in employing it in future Django projects. However, I'm wondering if anyone has encountered any drawbacks or gotchas when using Jinja2 templates with Django? If so, how did you work around them?

I wouldn't mind hearing about positive experiences either, just to get a good cross section of the best and worst of Jinja2.

like image 858
Soviut Avatar asked Jan 06 '09 22:01

Soviut


People also ask

Does Django use Jinja2?

Jinja is officially supported by Django, and even before that there were third-party packages that allowed you to use it. The only real compatibility issue is that you can't use Django's custom template tags in a Jinja template.

Is Jinja2 fast?

Jinja2 is fast, secure and designer friendly templating language for Python and Django. Jinja2 is a templating language for Python with powerful set of tools and intuitive design. It is often used to replace the default template engine for Django and was initially modelled after it.


1 Answers

I use Jinja2 in some of my projects and love the extra expressiveness it gives me. I can keep my presentation logic and application logic separate, but I don't have to bend over backwards to call into a function/method I've designed specifically for my presentation layer.

In addition to what's already been listed by other posters, here are some things that I've found:

  • The Admin app is tightly coupled to Django templates
  • The default views and decorators that come with the Auth app (and elsewhere) are coupled to Django templates, so you may have to duplicate the effort if you want to use your Jinja2 templates for login/logout/etc

Behaviorally, Django templates will escape its output by default whereas Jinja2 will not. I think either approach has its own merits, but you have to keep this in mind if you are switching between the two.

like image 174
Joe Holloway Avatar answered Sep 23 '22 15:09

Joe Holloway