在我的一个项目中,正在
上搜索模板<project_root>/<app>/templates/
正在另一个搜索中
<project_root>/<app>/templates/<app>/
这究竟取决于什么
两个项目的settings.py在settings.py中都具有以下完全相同的配置
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'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',
],
},
}, ]
这是错误
> TemplateDoesNotExist at /printer_add printer_add.html Request
> Method: GET Request URL: http://127.0.0.1:8000/printer_add Django
> Version: 3.0.3 Exception Type: TemplateDoesNotExist Exception Value:
> printer_add.html Exception
> Location: /usr/local/lib/python3.6/site-packages/django/template/loader.py
> in select_template, line 47 Python Executable: /usr/bin/python3 Python
> Version: 3.6.8 Python Path: ['/opt/app/w_apps',
> '/usr/lib64/python36.zip', '/usr/lib64/python3.6',
> '/usr/lib64/python3.6/lib-dynload',
> '/usr/local/lib64/python3.6/site-packages',
> '/usr/local/lib/python3.6/site-packages',
> '/usr/lib64/python3.6/site-packages',
> '/usr/lib/python3.6/site-packages', '/opt/app/w_apps'] Server
> time: Fri, 13 Mar 2020 10:38:48 +0000 Template-loader postmortem
> Django tried loading these templates, in this order:
>
> Using engine django:
>
> django.template.loaders.app_directories.Loader:
> /usr/local/lib/python3.6/site-packages/django/contrib/admin/templates/printer_add.html
> (Source does not exist)
> django.template.loaders.app_directories.Loader:
> /usr/local/lib/python3.6/site-packages/django/contrib/auth/templates/printer_add.html
> (Source does not exist)
> django.template.loaders.app_directories.Loader:
> /opt/app/w_apps/sts/templates/printer_add.html (Source does not exist)
处理视图为
class PrinterCreate(CreateView):
model = Printer
form_class = PrinterCreateForm
template_name = 'printer_add.html'
success_url = reverse_lazy('printer_list')
答案 0 :(得分:0)
您要在/opt/app/w_apps/sts/templates/sts/printer_add.html
中使用模板
回溯显示它正在/opt/app/w_apps/sts/templates
目录中查找。这是因为您的'APP_DIRS': True
设置中有TEMPLATES
。
如果将模板位置分为两个,则会得到:
/opt/app/w_apps/sts/templates/sts/printer_add.html = /opt/app/w_apps/sts/templates/ + sts/printer_add.html
因此,您应该在视图中使用sts/printer_add.html
作为模板名称。
class PrinterCreate(CreateView):
...
template_name = 'sts/printer_add.html'