问题描述
我已经安装了jinja2,然后"DIRS"停止工作(我必须手动添加它们).更改"APP_DIRS"无济于事
I have installed jinja2 and after that 'DIRS' stopped working(I have to include them by hand).Changing 'APP_DIRS' doesn`t help
模板看起来像这样:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
'APP_DIRS': False,
'DIRS': ['main/templates', 'shop/templates'],
'OPTIONS': {
'environment': 'django_test.create_jinjia_env.environment',
'autoescape': True,
'auto_reload': DEBUG,
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
如果不将模板包含在DIRS中,则会引发相同的错误
If don`t include templates into DIRS it throws the same error
找不到这样的问题.预先感谢!
Didn`t find the questions like that. Thanks in advance!
推荐答案
Django管理应用程序不附带Jinja模板.如果要使用Jinja和管理应用程序,则需要在TEMPLATES
设置中包括两个引擎:
The Django admin app does not come with Jinja templates. If you wish to use Jinja and the admin app, you need to include both engines in your TEMPLATES
setting:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True, # This allows Django to find the templates in the admin app
'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',
],
},
},
{
'BACKEND': 'django.template.backends.jinja2.Jinja2',
# The rest of your Jinja2 settings.
},
第二,当APP_DIRS
为True时,Jinja2后端在jinja2
子目录中查找模板.这意味着您应该将模板放在main/jinja2
和shop/jinja2
中,而不要放在main/templates
和shop/templates
中.
Secondly, when APP_DIRS
is True, the Jinja2 backend looks for templates in a jinja2
subdirectory. That means you should put your templates in main/jinja2
and shop/jinja2
instead of main/templates
and shop/templates
.
这篇关于在/admin/下安装jinja2 TemplateDoesNotExist之后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!