Mic*_*ael 2 python django jinja2 django-settings
我已经安装了jinja2,之后'DIRS'停止工作(我必须手工包含它们).改变'APP_DIRS'没有帮助
模板看起来像这样:
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',
],
},
},
]
Run Code Online (Sandbox Code Playgroud)
如果不在DIRS中包含模板,则会抛出相同的错误
没有找到那样的问题.提前致谢!
Django管理员应用程序没有Jinja模板.如果您想使用Jinja和管理员应用程序,则需要在您的TEMPLATES设置中包含两个引擎:
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.
},
Run Code Online (Sandbox Code Playgroud)
其次,当APP_DIRS为True时,Jinja2后端在jinja2子目录中查找模板.这意味着您应该将模板放入main/jinja2而shop/jinja2不是main/templates和shop/templates.