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

Django TemplateDoesNotExist错误:模板文件夹路径异常排查

Django模板找不到问题的解决

我正在学习CS50W的Django课程,之前跟着Brian敲代码都正常,现在不管怎么操作都一直报同一个错。我想通过Django渲染一个简单的HTML5页面,虽然我觉得已经指定了模板文件夹和index.html的正确路径,但Django却跑到hello/templates/目录下去找newyear/index.html。

视图代码

from django.shortcuts import render
from datetime import datetime


# Create your views here.
def index(request):
    now = datetime.now()
    return render(request, 'newyear/index.html', {
        'newyear': now.month == 1 and now.day == 1
    })

错误信息

Template loader postmortem
Django tried loading these templates, in this order:

Using engine django:
    * django.template.loaders.app_directories.Loader: 
/home/root/cs50w/week3-Django/lecture3/hello/templates/newyear/index.html 
(Source does not exist)

    * django.template.loaders.app_directories.Loader: 
/usr/local/lib/python3.10/dist-packages/django/contrib/admin/templates/newyear/index.html 
(Source does not exist)

    * django.template.loaders.app_directories.Loader: 
/usr/local/lib/python3.10/dist-packages/django/contrib/auth/templates/newyear/index.html 
(Source does not exist)



Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/usr/local/lib/python3.10/dist-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/root/cs50w/week3-Django/lecture3/newyear/views.py", line 8, in index
    return render(request, 'newyear/index.html', {
  File "/usr/local/lib/python3.10/dist-packages/django/shortcuts.py", line 24, in render
    content = loader.render_to_string(template_name, context, request, using=using)
  File "/usr/local/lib/python3.10/dist-packages/django/template/loader.py", line 61, in render_to_string
    template = get_template(template_name, using=using)
  File "/usr/local/lib/python3.10/dist-packages/django/template/loader.py", line 19, in get_template
    raise TemplateDoesNotExist(template_name, chain=chain)

Exception Type: TemplateDoesNotExist at /newyear/
Exception Value: newyear/index.html

项目目录结构

Lecture 3/
├── lecture3/
│   ├── __init__.py
│   ├── asgi.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
├── hello/
│   ├── templates/
│   │   └── hello/
│   │       ├── greet.html
│   │       └── index.html
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── newyear/
│   ├── template/
│   │   └── newyear/
│   │       └── index.html
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
└── manage.py

解决方案

问题出在newyear应用的模板文件夹名称错误:你把templates写成了template(少了一个s),Django默认只会识别应用目录下名为templates的文件夹作为模板目录,所以找不到你的newyear/index.html。

修复步骤:

  1. 将newyear/template/重命名为newyear/templates/,确保目录结构为newyear/templates/newyear/index.html
  2. 确认项目根目录的settings.py中,INSTALLED_APPS列表里已经添加了newyear,这样Django才能扫描到这个应用的模板目录

内容的提问来源于stack exchange,提问作者Gabriel Krupa

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 04:55:17