Django集成Tailwind时触发TemplateDoesNotExist错误无法加载main应用模板
Django TemplateDoesNotExist 报错修复方案
问题根因
- Django 默认会扫描已注册应用下的
templates(复数)目录加载模板,你当前main应用下的模板目录命名为template(单数),不符合默认约定,即便开启APP_DIRS = True也无法自动识别 - 你在
settings.py中填写的DIRS路径为普通字符串,Windows系统下的反斜杠会被识别为转义字符,导致路径读取失效
修复操作
- 重命名
main应用下的template文件夹为templates,确保最终目录结构为main > templates > main > home.html - 修改
settings.py中TEMPLATES配置项的DIRS参数,使用BASE_DIR拼接相对路径,避免硬编码绝对路径:
TEMPLATES = [ { 'BACKEND': 'django.template.backends.django.DjangoTemplates', 'DIRS': [], # 改完目录名后这里留空即可,APP_DIRS会自动扫描应用下的templates目录 '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', ], }, }, ]
如果你不想修改模板目录名,直接修正DIRS的路径写法即可:
'DIRS': [r'C:\Users\Kaij\Documents\djangoTests\djangoTailwind2\env\project\main\template'],
- 重启Django开发服务器,即可正常加载页面。
内容的提问来源于stack exchange,提问作者Katie Melosto
相关产品推荐
相关产品推荐

