Configuring Jinja2 in a Django project

A question with a newbie, but I can’t find enough step-by-step instructions on the official Jinja2 website or on Google.

My current Django project is halfway through, and I can no longer use the query filter in the Django template. So decide to switch to Jinja2.

What I did: pip install jinja2 for virtualenv. Then I tried to use Jinja syntax in template files, which does not work. Apparently and sad

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

+6
source share
2 answers

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

+4
source

I just want to recommend two alternatives that are very useful to me.

Jinja Template Tag

One of them is the jinja template tag, which allows you to use jinja in django templates as

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

Very useful when you cannot / do not want to split other existing templates.

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

Conditional Template Engine

Another is the conditional loading of template engines based on the file name (note that I wrote this since I use both template languages):

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

+9
source

Source: https://habr.com/ru/post/946701/


All Articles