【发布时间】:2020-07-16 06:25:03
【问题描述】:
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return render(request, 'templates/index.html')
但我的页面显示 TemplateDoesNotExist。
我该怎么办?
【问题讨论】:
from django.http import HttpResponse
from django.shortcuts import render
def home(request):
return render(request, 'templates/index.html')
但我的页面显示 TemplateDoesNotExist。
我该怎么办?
【问题讨论】:
在您的justColor 文件夹中创建一个templates\justColor 子目录模板并将index.html 移动到那里。然后正如@Neeraj 所说,您可以将渲染调用更改为render(request, 'justColor/index.html')。
(尽管您应该阅读 django 教程的这一部分,这将比在这个主题中的任何试错法对您更有帮助:https://docs.djangoproject.com/en/3.0/intro/tutorial03/)
【讨论】:
DIRS 上的变量settings.py 以添加模板目录,对吧?当您在设置文件上启用应用程序时,我的回答是次要选项。
确保您已正确配置 urls.py 和 views.py。
这个问题已经在这里回答了:Django TemplateDoesNotExist?
【讨论】:
在模板目录下创建一个文件夹'justcolor'并将index.html保存在其中。将路径更改为“justcolor/index.html”。返回渲染(请求,'justcolor/index.html')
【讨论】:
templates 最好是应用程序的子目录,而不是项目的子目录。而且我认为模板路径也区分大小写,所以它是justColor/index.html