pip install jinja2
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',# 修改模板引擎
'DIRS': [os.path.join(BASE_DIR,'templates')],# 设置模板路径
'APP_DIRS': True,
'OPTIONS': {
'environment':'utils.jinja2_enviroment.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',
],
},
},
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(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',
],
},
}
]
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
自此jinja2模板配置配置完成