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
时间: 2025-05-31 10:55:49 AIGC 浏览: 81
### Django 开发中的常见问题分析
在 Django 应用程序开发过程中,遇到 `MultiValueDictKeyError` 和 `TemplateDoesNotExist` 错误是比较常见的现象。以下是针对这些问题的具体原因和解决方案。
#### 1. **关于 MultiValueDictKeyError 'choice'**
此错误通常发生在尝试访问请求参数字典 (`request.POST`) 中不存在的键时。Django 的 `QueryDict` 是一种特殊的字典结构,在处理表单提交数据时会抛出此类异常。如果试图通过索引方式获取一个未定义的键,则会出现该错误。
解决方法如下:
- 使用 `.get()` 方法代替直接索引来安全地检索值[^2]。
```python
selected_choice = request.POST.get('choice', None)
if not selected_choice:
# 处理缺失的情况
pass
```
这样可以防止因缺少指定字段而导致的应用崩溃。
#### 2. **关于 TemplateDoesNotExist polls/detail.html**
当渲染视图时找不到对应的 HTML 文件路径时会发生这个错误。这可能是由于以下几个原因之一造成的:
- 模板文件名拼写有误;
- 设置了不正确的目录配置项(即 TEMPLATES 配置部分);
- 或者项目根目录下的 templates 子文件夹中确实缺乏所需的 .html 文件。
确认无误之后再重新加载页面即可消除报错信息[^3]。
调整设置示例代码片段如下所示:
```python
TEMPLATES = [
{
...
'DIRS': [BASE_DIR / 'templates'], # 确保这里指向实际存在的位置
...
},
]
```
#### 3. **有关于 HTTP 404 Errors Solutions**
HTTP状态码404意味着客户端请求了一个服务器无法找到资源的位置链接地址。对于这种情况可以从多个角度去排查并修复它,比如检查URLconf规则匹配情况、静态媒体服务状况等等[^4].
一些可能的方向包括但不限于:
- 定义清晰全面覆盖各种可能性模式表达式的urlpatterns列表;
- 对应view函数内部逻辑实现正确返回HttpResponse对象而不是NoneType;
下面给出一段简单的urls.py例子供参考学习之用:
```python
from django.urls import path
from . import views
urlpatterns = [
path('', views.index, name='index'),
path('<int:pk>/', views.detail, name='detail'), # 动态路由捕获变量
]
```
---
### 总结说明
以上分别就三个主要方面阐述了解决方案思路及其操作步骤指导。希望对你有所帮助!
阅读全文
相关推荐



















