Django模板不存在的处理方法
不存在的模板
在Django中,当运行以下代码时出现了上述错误。
from django.template.response import TemplateResponse
def product_list(request):
return TemplateResponse(request, 'catalogue/product_list.html')
应该怎么做呢?需要查看的地方在setting.py文件的两个位置。
使用模板的路径
请确认是否如下所述。
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'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',
],
},
},
]
如果是这样那就没问题了。默认应该是这样的,如果没有进行过更改,应该也没问题。
2.安装的应用程序
我错误的原因在这里。
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'ec.catalogue',
]
需要在INSTALLED_APPS中注册项目中的所有应用程序。
也就是说,需要添加包含views.py和urls.py的位置。
因此,通过添加包含应用程序的位置(例如,ec.catalogue)可以使用模板。