Replace Django's default template engine with jinja2's implementation
- 2020-10-07 18:46:25
- OfStack
The native environment
Operating system: fedora24
python version: 3.5
Django version: 1.11.1
jinja2 version: 2.9.6
Why replace it
DTL
Let's talk about the template engine of Django. There is no definite name for it, so it is simply called Django Templates Language(DTL). In Chinese, it is called Django template language. As you use it, you'll see a lot of limitations, the most obvious of which is four operations. You can only add or subtract, not multiply or divide. In addition, the judgment is equal, if should be used instead of ifequal. It's really not very convenient. One more point. DTL is slow. jinja2 claims to be 10-20 times faster than DTL.
jinja2
So let's install pip3, install jinja2.
Configuration: Starting with Django1.8, it supports a third party template engine and can be easily configured to. First, create 1 jinja2_ES43en.py under your project, which reads as follows:
#from __future__ import absolute_import # If it is py2 Just uncomment this line
from django.contrib.staticfiles.storage import staticfiles_storage
from django.urls import reverse
from jinja2 import Environment
def environment(**options):
env = Environment(**options)
env.globals.update({
'static': staticfiles_storage.url,
'url': reverse,
})
return env
Then set in setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2', ***1
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': False, ***2
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
'environment': 'APP_NAME.jinja2_env.environment', ***3
},
},
]
Those marked *** are the places that need to be modified.
- ***1: Specify engine jinja2 here
- ***2: Make sure you don't look for templates in the jinja2 subdirectory under app
- ***3: Configure the environment, py file created above
This way, your default template engine will be added.
1 change to jinja2
Toggle your ide template language
For example I use pycharm, in ES74en-ES75en-ES76en & Modified in ES78en-ES79en Template Languages.
This step is not necessary, but the wrong supporting template language will make you think that the correct tag is wrong.
csrf_token
Django:
{% csrf_token %}
jinja2
{{ csrf_input }}
or
<input type="hidden" name="csrfmiddlewaretoken" value="{{ csrf_token }}">
4 the operation
Django: It only supports addition and subtraction
{{ var | add:-1 }}
jinja2 : 4 The operation
{{ var + 1 }}
The if condition determines equality
Django:
{% ifequal vara varb %}do sometings{% endifequal %} # equal
{% ifnotequal vara varb %}do sometings{% endifnotequal %} # Ranging from
jinja2 :
{% if vara == varb %}do sometings{% endif %} # equal
{% if vara ! = varb %}do sometings{% endif %} # Ranging from
jinja2 is more in line with python syntax
Loop index
Django:
{% for i in s %}
{{ forloop.counter }} # from 1 Initial index
{% endfor %}
jinja2
{% for i in s %}
{{ loop.index }} # from 1 Initial index
{{ loop.index0 }} # from 1 Initial index
{% endfor %}
Support for methods
In Django, methods are attributes that are treated as objects
In jinja2, it's just a method, so you can also pass parameters.
Let's say a form object has an as_p() method.
Django :
{{ form.as_p }}
jinja2:
{{ form.as_p() }}
Print variables that do not exist
In Django, if no dictionary parameter is passed, using {{var}} will print 1 blank (nothing)
But jinja2, if the value does not exist, it will print out "{{var}}". To achieve functions like DTL, please use {{var|default(" ")}}.
1 some pit
Do not pass in a parameter named user
Measured in the template, itself has variable user, is pointing django contrib. auth. models. User, if you pass a dictionary keys for user, actual is equivalent to the incoming, useless, so, had its name changed.