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

明明存在模板,Django却抛出TemplateDoesNotExist错误该如何解决?

Django模板找不到(TemplateDoesNotExist)问题解决方法

错误原因

你遇到的TemplateDoesNotExist: myfirst.html是因为Django找不到指定的模板文件,核心问题是模板的存放路径不符合Django的默认查找规则。

解决步骤

1. 修正模板文件夹结构

在你的members应用目录下,必须创建一个名为templates的文件夹,然后把myfirst.html放在这个文件夹内。正确的目录结构应该是:

myworld/
  members/
    templates/
      myfirst.html
    views.py
    # 其他应用文件(如models.py、apps.py等)

2. 确认应用已注册到项目

打开项目根目录的settings.py文件,检查INSTALLED_APPS列表中是否包含members,确保它被正确注册:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'members',  # 这一行必须存在
]

3. 优化视图代码(可选)

Django提供了更简洁的render函数,可替代你当前使用的loader+HttpResponse写法,代码更易读且不易出错:

from django.shortcuts import render

def index(request):
    return render(request, 'myfirst.html')

完成以上步骤后,重启Django开发服务器,访问/members路径就能正常渲染你的HTML内容了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 14:41:26