Django运行报错TemplateDoesNotExist: Home.html 问题求助
问题原因
- 模板引用路径和实际存放路径不匹配:你将
Home.html存放在customer/templates/customer/子目录下,但视图函数中调用render时只写了Home.html。Django开启APP_DIRS = True配置后,只会扫描每个已注册应用templates目录的根层级文件,不会自动遍历子目录。从报错日志也可验证:Django最后查找的路径是web_project\customer\templates\Home.html,和实际存放路径差了一级customer子目录,因此匹配失败。 - 可选排查点:请先确认
settings.py的INSTALLED_APPS列表中已经添加了customer应用,未注册的应用不会被Django扫描模板目录。
修复方案
两种方案二选一即可:
方案1:修改视图模板引用路径(更推荐,可避免不同应用间模板重名冲突)
修改customer应用下views.py的模板路径,加上应用名前缀:
from django.shortcuts import render def Home(request): return render(request, "customer/Home.html", {"title":" Home"}) def Hall(request): return render(request, "customer/Hall.html", {"title": "Hall"}) def Food_item(request): return render(request, "customer/Food_item.html", {"title": "Food_item"}) def About_us(request): return render(request, "customer/About_us.html", {"title": "About_us"})
方案2:调整模板存放位置
将customer/templates/customer/目录下的所有HTML模板文件,直接移动到customer/templates/根目录下,无需修改现有视图代码。
内容的提问来源于stack exchange,提问作者Nawaf
相关产品推荐
相关产品推荐

