Django报错解决:TemplateDoesNotExist at /polls/

1. 问题介绍

在Django4.1的官网教程——编写你的第一个 Django 应用,第 3 部分,按部就班地走下来,会报错:

 下面还有提示:

2. 问题思考

从提示得知,是我们写的django-mysite(工程名)/templates/polls/index.html和 django-mysite(工程名)/templates/polls/detail.html不存在,因为python解释器去Python安装路径下的lib/site-packages/django/...去搜索了。

很明显,我们的项目并没有在Python安装目录中创建。

所以解决思路也就清晰多了:修改django检索index.html的目录。

3. 寻找答案

在下面这个网站中,我找到了答案,请看【解决方案6】

TemplateDoesNotExist 在 /polls/答案 - 爱码网 (likecs.com)

4. 解决问题

具体操作是:

修改django-mysite(工程名)/mysite/settings.py中的TEMPLATES(模板)的配置,使其默认地址为当前工程路径。

即在合适的位置加这些代码:
 

import os

# 这个变量默认是没有的,建议在默认的TEMPLAETS变量前面一行定义
TEMPLATE_DIR = os.path.join(BASE_DIR,'templates')

# 然后在TEMPLATES变量中的'DIRS':[]的括号里,加上刚定义的变量,即
TEMPLATES = [
    {
        # 前面省略
        'DIRS': [TEMPLATE_DIR],
        # 后面省略
    }
]

然后在浏览器中输入127.0.0.1:8000/polls/,就不会报错了

 

C:\Users\ASUS\myproject\myapp\.venv\Scripts\python.exe K:\专业\py\PycharmProjects\mysite\manage.py runserver 8000 Performing system checks... Watching for file changes with StatReloader System check identified some issues: WARNINGS: ?: (staticfiles.W004) The directory 'K:\专业\py\PycharmProjects\mysite\static' in the STATICFILES_DIRS setting does not exist. System check identified 1 issue (0 silenced). May 24, 2025 - 17:08:15 Django version 5.2.1, using settings 'mysite.settings' Starting development server at http://127.0.0.1:8000/ Quit the server with CTRL-BREAK. WARNING: This is a development server. Do not use it in a production setting. Use a production WSGI or ASGI server instead. For more information on production servers see: https://docs.djangoproject.com/en/5.2/howto/deployment/ Not Found: / [24/May/2025 17:08:18] "GET / HTTP/1.1" 404 2306 Not Found: /img/new_ico.0750a9ab.png Not Found: /img/ico.png [24/May/2025 17:08:18] "GET /img/new_ico.0750a9ab.png HTTP/1.1" 404 2396 [24/May/2025 17:08:18] "GET /img/ico.png HTTP/1.1" 404 2357 [24/May/2025 17:09:18] "GET /polls/1/vote HTTP/1.1" 301 0 Internal Server Error: /polls/1/vote/ Traceback (most recent call last): File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\utils\datastructures.py", line 84, in __getitem__ list_ = super().__getitem__(key) ^^^^^^^^^^^^^^^^^^^^^^^^ KeyError: 'choice' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "K:\专业\py\PycharmProjects\mysite\polls\views.py", line 41, in vote selected_choice = question.choice_set.get(pk=request.POST['choice']) ~~~~~~~~~~~~^^^^^^^^^^ File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\utils\datastructures.py", line 86, in __getitem__ raise MultiValueDictKeyError(key) django.utils.datastructures.MultiValueDictKeyError: 'choice' During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\core\handlers\exception.py", line 55, in inner response = get_response(request) ^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\core\handlers\base.py", line 197, in _get_response response = wrapped_callback(request, *callback_args, **callback_kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "K:\专业\py\PycharmProjects\mysite\polls\views.py", line 44, in vote return render(request, "polls / detail.html", { ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\shortcuts.py", line 25, in render content = loader.render_to_string(template_name, context, request, using=using) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\template\loader.py", line 61, in render_to_string template = get_template(template_name, using=using) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\myproject\myapp\.venv\Lib\site-packages\django\template\loader.py", line 19, in get_template raise TemplateDoesNotExist(template_name, chain=chain) django.template.exceptions.TemplateDoesNotExist: polls / detail.html [24/May/2025 17:09:18] "GET /polls/1/vote/ HTTP/1.1" 500 99573 Not Found: /img/new_ico.0750a9ab.png [24/May/2025 17:09:18] "GET /img/new_ico.0750a9ab.png HTTP/1.1" 404 2396 Not Found: /polls/1/vote/img/ico.png [24/May/2025 17:09:18] "GET /polls/1/vote/img/ico.png HTTP/1.1" 404 3328
最新发布
05-26
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值