You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

配置正确仍遇Django TemplateDoesNotExist错误的排查求助

解决Django TemplateDoesNotExist错误(针对playground/hello.html)

最常见问题:模板文件路径不符合Django查找规则

当APP_DIRS=True时,Django会自动扫描已注册app的templates目录,但为了避免不同app的模板重名冲突,规范的做法是在templates目录下再创建一个与app同名的子目录,将模板文件放入其中。

你的项目里,当前playground/templates目录缺少同名子目录,正确的文件路径应该是:

playground/templates/playground/hello.html

调整路径后,你当前的render(request, 'playground/hello.html')写法就能正确匹配到模板文件。

检查Playground app是否已注册到项目

如果app未在settings.py的INSTALLED_APPS中注册,Django不会扫描它的templates目录。打开storefront/storefront/settings.py,找到INSTALLED_APPS列表,添加以下内容:

INSTALLED_APPS = [
    # 保留默认的app(如django.contrib.admin等)
    'playground',  # 或写'playground.apps.PlaygroundConfig',对应apps.py中的配置类
]

默认生成的playground/apps.py中配置类为PlaygroundConfig,两种写法均有效,后者更规范。

排查拼写与大小写问题

  • 确认模板文件名为hello.html,而非hello.htm或Hello.html(Linux/macOS系统对文件名大小写敏感)
  • 确认路径拼写无误,没有多写/少写斜杠

额外验证方法(可选)

运行以下命令查看Django当前识别的所有模板目录,确认playground/templates是否在列表中:

python manage.py inspectdb --template-dirs

如果输出中没有该目录,说明app未注册或路径配置存在问题。


内容的提问来源于stack exchange,提问作者majed khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.30 11:26:05