我想知道django在哪里搜索index.html
Setting.py
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['templates'],
'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',
],
},
},
]
views.py
def index(request):
return render(request, 'index.html', {})
答案 0 :(得分:1)
Django将在templates目录中查找,即是否创建了一个名为templates的文件夹,以及在templates文件夹中是否有一个名为index.html的文件。出于某种原因,我觉得您问这个问题的原因是因为在运行服务器时无法渲染模板,对吗?请回到我身边,我会尽力填补您的空白,以便使您的项目正常进行。
答案 1 :(得分:0)
您可以轻松解决此问题:如果您的项目目录(即manage.py所在的目录)的名称为“ RalphPortfolio”,则需要在settings.py中进行以下更正:
'DIRS': ['RalphPortfolio/templates'],
答案 2 :(得分:0)
添加模板目录的完整路径 例如,如果您的路径是 /home/some_url/your_project/templates
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': ['/home/some_url/your_project_name/templates'],
'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',
],
},
},
]
是因为路径不清晰,找不到。