首页 > 解决方案 > 找不到 Django 404 错误页面...当前路径与最后一个匹配

问题描述

我是 django 的新手,我正在玩自己的民意调查教程变体。我的应用程序运行良好,每个页面都正确加载。然后我做了一个改变;我创建然后删除了一个模型。现在,许多网址似乎都坏了。而且我不明白这个错误:

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/1/pick/
Raised by:  polls.views.pick
Using the URLconf defined in mySite.urls, Django tried these URL patterns, in this order:

polls/ [name='index']
polls/ <int:pk>/ [name='detail']
polls/ <int:pk>/results/ [name='results']
polls/ <int:question_id>/vote/ [name='vote']
polls/ <int:question_id>/tally/ [name='tally']
polls/ <int:question_id>/pick/ [name='pick']
The current path, polls/1/pick/, matched the last one.

You’re seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

所以我不明白为什么会这样说:当前路径 polls/1/pick/ 与最后一个匹配。

也找不到页面?这是如何运作的?当应用程序之前运行良好时,可能是什么导致了这种情况?

我的 urls.py:

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'),
    path('<int:question_id>/tally/', views.tally, name='tally'),
    path('<int:question_id>/pick/', views.pick, name='pick'),
    path('<int:pk>/video/', views.VideoView.as_view(), name='video'),
    path('<int:pk>/reveal/', views.RevealView.as_view(), name='reveal'),
]

并查看片段:

def pick(request, question_id):
    question = get_object_or_404(Question, pk=question_id)
    player = get_object_or_404(Player, pk=1)
    question.score = player.score
    question.save()
    return HttpResponseRedirect(reverse('polls:video', args=(question.id,)))

标签: djangohttp-status-code-404

解决方案


这行错误

Raised by:  polls.views.pick

告诉您由于找不到匹配的 URL 而没有得到 404,得到 404 是因为您的视图函数polls.views.pick引发了 404。该函数中有两行可能引发 404

question = get_object_or_404(Question, pk=question_id)
player = get_object_or_404(Player, pk=1)

因此,在您的数据库中,您要么没有 pk=1 的问题,要么没有 pk=1 的玩家。


推荐阅读