jinja2 module of Django template

Jinja2: Python is a widely used template engine, is implemented by Python template language, his design ideas from Django template engine, and expanded its syntax and a powerful set of features, especially Flask framework built-in template language.

Because django default template engine function is not complete, slow, so we can also use jinja2 in Django, jinja2 declared default template engine django than 10-20 times faster.

Django popular third-party APP basically supports both Django's default template and jinja2, so there will not be much use jinja2 obstacles.

Use jinja2 template process:

1. Install third-party modules:

pip intall jinja2 -i mirroring

When you install, it is best to specify the source image, otherwise domestic walls too high to climb over how much more will receive a little slow.

2. Configure jinja2:

  2.1 jinja2 file created in Django project and create the environment object:

from jinja2 import Environment

def environment(**options):
    env = Environment(**options)

    return env

  2.2 jinja2 template configuration information in Django project configuration file:

note:

  Profiles Profiles Django project information that is generally named setting.py, dev.py, prop.py.

  When you configure jinja2 template in the configuration file, you need to first Django template configuration file that comes with configuration information or delete the comment.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.jinja2.Jinja2',#修改1
        'DIRS': [os.path.join(BASE_DIR, 'templates')],
        'APP_DIRS':True,
        'OPTIONS':{
            'environment': 'jinja2_env.environment',# 修改2
            'context_processors':[
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

3. Use jinja2 render html template:

  3.1 preparation of view, the return jinja2 template, and pass the data to render.

class IndexView(View):
    
    def get(self, request):
        context = {
            'key':'value',
            'msg':'Hello Jinja2'
        }
        return render(request, 'index.html', context)

  3.2 jinja2 syntax data received in the rear end of the transferred HTML template

{% if key%}
    <span class="error_tip">{{ key}}</span>
{% endif %}

{% if msg%}
    <span class="error_tip">{{ msg}}</span>
{% endif %}

4. jinja2 filter:

5. Custom filters jinja2

In jinja2 environment file, customized filter jinja2:

from jinja2 import Environment

def environment(**options):
    env = Environment(**options)

    # 2 from the filter is added to the environment defined 
    env.filters [ ' do_listreverse ' ] = do_listreverse

    return env

# 1. custom filters 
DEF do_listreverse (Li):
     IF Li == " B " :
         return  " ha "

Guess you like

Origin www.cnblogs.com/chao666/p/12178043.html