jinja2配置

base_jinja2.py

#coding:utf-8

from jinja2 import Environment  # jinja2的虚拟环境
from django.contrib.staticfiles.storage import staticfiles_storage  # django的一些依赖
from django.urls import reverse  # urls需要和jinja进行一些配置


def environment(**options):
    env = Environment(**options)  # 把一些配置文件添加进去
    env.globals.update({
        'static': staticfiles_storage.url,
        'url': reverse
    })
    return env

setting.py

TEMPLATES = [
    {
        # 配置jinja2的模板引擎
        "BACKEND": "django.template.backends.jinja2.Jinja2",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "environment": "project01.base_jinja2.environment",
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
                # 'django.core.context_processors.media',
            ],
        },
    },
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "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",
                # 'django.core.context_processors.media',
            ],
        },
    },
]

原文链接: jinja2配置 版权所有,转载时请注明出处,违者必究。
注明出处格式:流沙团 ( https://gyarmy.com/post-748.html )

发表评论

0则评论给“jinja2配置”