TemplateDoesNotExist 异常

本文介绍了如何解决Django中TemplateDoesNotExist异常的问题。当Django无法找到指定的模板文件时会抛出此异常。文章详细解释了在Windows环境下正确配置TEMPLATE_DIRS的方法,即使用Unix风格的路径分隔符。

TemplateDoesNotExist 异常

如果 get_template() 找不到给定名称的模板,将会引发一个 TemplateDoesNotExist 异常。
假设你的 DEBUG项设置为 True,而你有没有建立current_datetime.html 这个模板文件,你会看到Django的错误提示网页,告诉你发生了 TemplateDoesNotExist 错误
在建立了这个html文件后,又报了同样的异常,害我搞了好久,才发现问题发生在
TEMPLATE_DIRS = (
    '\Python26\Scripts\mysite\templates',
)
注意:

Windows用户必须使用斜杠而不是反斜杠。 get_template() 假定的是 Unix 风格的文件名符号约定。

修改为:

TEMPLATE_DIRS = (
    '/Python26/Scripts/mysite/templates',
)
接下来运行就OK了。

转载于:https://www.cnblogs.com/nbalive2001/p/3327836.html

再打开网页结果:TemplateDoesNotExist at /accounts/login/ registration/login.html Request Method: GET Request URL: http://localhost:8000/accounts/login/ Django Version: 5.2.6 Exception Type: TemplateDoesNotExist Exception Value: registration/login.html Exception Location: E:\python_work\learning_log\ll_env\Lib\site-packages\django\template\loader.py, line 47, in select_template Raised during: django.contrib.auth.views.LoginView Python Executable: E:\python_work\learning_log\ll_env\Scripts\python.exe Python Version: 3.13.6 Python Path: ['E:\\python_work\\learning_log', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\python313.zip', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\DLLs', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\Lib', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313', 'E:\\python_work\\learning_log\\ll_env', 'E:\\python_work\\learning_log\\ll_env\\Lib\\site-packages'] Server time: Thu, 11 Sep 2025 03:42:26 +0000 Template-loader postmortem Django tried loading these templates, in this order: Using engine django: django.template.loaders.app_directories.Loader: E:\python_work\learning_log\learning_logs\templates\registration\login.html (Source does not exist) django.template.loaders.app_directories.Loader: E:\python_work\learning_log\ll_env\Lib\site-packages\django\contrib\admin\templates\registration\login.html (Source does not exist) django.template.loaders.app_directories.Loader: E:\python_work\learning_log\ll_env\Lib\site-packages\django\contrib\auth\templates\registration\login.html (Source does not exist)
09-12
按照书中一步一步做下来还是报错:TemplateDoesNotExist at /accounts/login/ registration/login.html Request Method: GET Request URL: http://localhost:8000/accounts/login/ Django Version: 5.2.6 Exception Type: TemplateDoesNotExist Exception Value: registration/login.html Exception Location: E:\python_work\learning_log\ll_env\Lib\site-packages\django\template\loader.py, line 47, in select_template Raised during: django.contrib.auth.views.LoginView Python Executable: E:\python_work\learning_log\ll_env\Scripts\python.exe Python Version: 3.13.6 Python Path: ['E:\\python_work\\learning_log', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\python313.zip', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\DLLs', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313\\Lib', 'E:\\Users\\Administrator\\AppData\\Local\\Programs\\Python\\Python313', 'E:\\python_work\\learning_log\\ll_env', 'E:\\python_work\\learning_log\\ll_env\\Lib\\site-packages'] Server time: Thu, 11 Sep 2025 04:31:17 +0000 Template-loader postmortem Django tried loading these templates, in this order: Using engine django: django.template.loaders.app_directories.Loader: E:\python_work\learning_log\learning_logs\templates\registration\login.html (Source does not exist) django.template.loaders.app_directories.Loader: E:\python_work\learning_log\ll_env\Lib\site-packages\django\contrib\admin\templates\registration\login.html (Source does not exist) django.template.loaders.app_directories.Loader: E:\python_work\learning_log\ll_env\Lib\site-packages\django\contrib\auth\templates\registration\login.html (Source does not exist)
最新发布
09-12
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值