首页 > 解决方案 > 为什么我会收到 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 很陌生,任何帮助将不胜感激!

标签: pythondjango

解决方案


推荐阅读