Django不';在templates/app目录中找不到模板
在我的一个项目中,正在搜索模板Django不';在templates/app目录中找不到模板,django,django-templates,Django,Django Templates,在我的一个项目中,正在搜索模板 <project_root>/<app>/templates/ 这里是错误 > 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: T
<project_root>/<app>/templates/
这里是错误
> 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')
您想在
/opt/app/w\u apps/sts/templates/sts/printer\u add.html
回溯显示它正在查看/opt/app/w_apps/sts/templates
目录。这是因为您的模板设置中有'APP\u DIRS':True
如果将模板位置拆分为两个,则会得到:
/opt/app/w_apps/sts/templates/sts/printer_add.html = /opt/app/w_apps/sts/templates/ + sts/printer_add.html
因此,您应该使用sts/printer\u add.html
作为视图中的模板名称
class PrinterCreate(CreateView):
...
template_name = 'sts/printer_add.html'
您尚未显示视图或错误消息。我怀疑在一个项目中,您有类似于return(request,“my_template.html”)
的内容,而在另一个项目中,您有return render(request,“app/my_template.html”)
。使用app/my_template.html
的优点是,多个应用程序可以有一个名称相同的模板,而不会发生冲突。我刚刚更新了原始问题中的错误。好的,那么处理/printer_add
的视图是什么?printer\u add.html
template在哪里?它在/opt/app/w\u apps/sts/templates/sts/文件夹中。问题中的视图已更新。
/opt/app/w_apps/sts/templates/sts/printer_add.html = /opt/app/w_apps/sts/templates/ + sts/printer_add.html
class PrinterCreate(CreateView):
...
template_name = 'sts/printer_add.html'