python - 为什么我会收到 Django Reslover404 错误?
问题描述
我正在用 djbook 学习 Django,并且到目前为止创建了我的“民意调查”应用程序。它工作正常,但是当我添加一个日志文件时,其中有一条错误消息:
Exception while resolving variable 'name' in template 'unknown'.
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/usr/lib/python3.8/site-packages/django/core/handlers/base.py", line 100, in _get_response
resolver_match = resolver.resolve(request.path_info)
File "/usr/lib/python3.8/site-packages/django/urls/resolvers.py", line 567, in resolve
raise Resolver404({'tried': tried, 'path': new_path})
django.urls.exceptions.Resolver404: {'tried': [[<URLResolver <URLPattern list> (admin:admin) 'admin/'>], [<URLResolver <module 'polls.urls' from '/home/DeadMoroz/mysite/polls/urls.py'> (polls:polls) 'polls/'>]], 'path': ''}
...
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/usr/lib/python3.8/site-packages/django/template/base.py", line 848, in _resolve_lookup
raise VariableDoesNotExist("Failed lookup for key "
django.template.base.VariableDoesNotExist: Failed lookup for key [name] in <URLResolver <module 'polls.urls' from '/home/DeadMoroz/mysite/polls/urls.py'> (polls:polls) 'polls/'>
Not Found: /
(0.011) SELECT "polls_question"."id", "polls_question"."question_text", "polls_question"."pub_date", "polls_question"."author" FROM "polls_question" WHERE "polls_question"."pub_date" <= '2020-04-28 04:40:16.152659' ORDER BY "polls_question"."pub_date" DESC LIMIT 5; args=('2020-04-28 04:40:16.152659',)
我的 'polls/urls.py'文件似乎与 Django Book 中的相同:
from django.urls import path
from . import views
app_name = 'polls'
urlpatterns = [
path('', views.IndexView.as_view(), name='index'),
path('<int:pk>/', views.DetailView.as_view(), name='detail'),
path('<int:pk>/results/', views.ResultsView.as_view(), name='results'),
path('<int:question_id>/vote/', views.vote, name='vote'),
]
这是我的项目文件结构:
polls/
---logs/
------debug.log
---migrations/
---static/
------polls/
---------images/
------------background.gif
------style.css
---templates/
------admin/
---------base_site.html
------polls/
---------detail.html
---------index.html
---------results.html
---__init.py__
---admin.py
---apps.py
---models.py
---tests.py
---urls.py
---views.py
我对 Django 很陌生,任何帮助将不胜感激!
解决方案
推荐阅读
- jquery - 显示标题和标题fancybox 3
- javascript - 我该如何编写脚本,如果找到想要的 .js 文件则显示消息然后执行它?
- html - 为什么包含 div 需要填充以使包含项目的边距位于 div 中?
- php - mariadb 从 php 保存所有 INSERT 信息两次
- javascript - Html5 打开一个新窗口而不是弹出窗口
- python - 如何在python的列中连接组中的值
- ruby-on-rails - Rails:@category 与 @categories
- jwt - 签名密钥、证书和客户机密混淆
- mysql - MariaDB 查询问题(ORDER BY 被忽略)
- html - flexbox 和屏幕尺寸的问题