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

Django开发身份认证程序报TemplateDoesNotExist: templates/index.html错误

Django模板不存在报错修复方案

问题根因说明

你的配置和路径存在两处核心问题,按以下步骤修改即可解决:

1. 注册自定义应用到Django配置

你当前settings.py的INSTALLED_APPS列表未添加自己开发的authentication应用,Django开启APP_DIRS=True时,只会扫描已注册应用下的templates目录查找模板,修改配置如下:

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'authentication', # 新增此行,注册你的身份认证应用
]

2. 修正模板路径配置与存放结构

两种模板存放方案二选一即可:

方案A:使用应用内模板目录(官方推荐)

在authentication应用文件夹下创建层级目录:templates/authentication/,把index.html、signin.html放到该目录下,最终路径结构:

gfg(项目根目录)
└── authentication
    └── templates
        └── authentication
            ├── index.html
            └── signin.html

方案B:使用项目根目录模板目录

如果你想把模板统一放在项目根目录的templates文件夹下,首先创建层级目录 templates/authentication/,把模板文件放入该目录,再修改settings.py的模板路径配置,用绝对路径避免解析错误:

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'], # 修改此行,用BASE_DIR拼接绝对路径
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

对应的路径结构:

gfg(项目根目录)
├── templates
│   └── authentication
│       ├── index.html
│       └── signin.html
└── authentication

3. 无需修改现有视图代码

你views.py中render方法的模板路径"authentication/index.html"写法正确,按以上方案调整后即可正常加载模板。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 07:09:05