Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup Jinja2 in Django Project

Newbie question, yet I cannot find a sufficient step-by-step instruction on Jinja2 official website or by googling.

My current Django project is halfway done, and I cannot stand not able to use queryset filter in Django template anymore. So decide to switch to Jinja2.

What I have done: pip install jinja2 to the virtualenv. Then I tried to use Jinja syntax in my template files, which fails.. apparently and sadly

What else do I need to do to make Jinja2 work?

like image 896
Philip007 Avatar asked Jun 06 '13 15:06

Philip007


2 Answers

I just want to recommend two alternatives I find very useful.

Jinja template tag

One is the jinja template tag which lets you use jinja within django templates as

{% jinja %}{{ this_is_jinja(True)}}{% endjinja %}

Very useful when you can't / don't want to break the rest of your existing templates.

http://www.mellowmorning.com/2010/08/24/mixing-django-with-jinja2-without-losing-template-debugging/

Conditional template engine

The other is conditional loading of template engines based on filename (note I wrote this since I use both template languages):

https://github.com/yuchant/django-jinja2

like image 118
Yuji 'Tomita' Tomita Avatar answered Nov 15 '22 06:11

Yuji 'Tomita' Tomita


You'll need to switch to using a rendering method that uses Jinja2 templates instead of Django templates - coffin has a helper you can use named render_to_response that should work.

like image 44
girasquid Avatar answered Nov 15 '22 08:11

girasquid