You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django 1.11 Polls应用配置视图时出现404错误求助

Fixing the 404 Error When Accessing polls/1 in Django 1.11

Hey there, let's break down why you're seeing that 404 error when trying to access http://localhost:8000/polls/1—it's all about how your URL patterns are set up!

Let's look at the error clue

From your 404 page, Django tells us it tried these URL patterns:

^polls/ ^$ [name='index']
^polls/ ^(?P<question_id>[0-9]+)/$, [name='detail']
^polls/ ^(?P<question_id>[0-9]+)/results/$, [name='results']
^polls/ ^(?P<question_id>[0-9]+)/vote/$, [name='vote']
^admin/

The key issue here is the detail pattern: ^(?P<question_id>[0-9]+)/$. The trailing /$ means it only matches URLs that end with a slash—so polls/1/ (with a slash at the end) would work, but polls/1 (without the slash) doesn't match any pattern.

Here are 3 ways to fix this:

  1. Use the trailing slash in your URL
    Just visit http://localhost:8000/polls/1/ instead of polls/1—this should immediately match the detail view as defined in your URLconf. This aligns with Django's default URL design convention (treating URLs like directories with trailing slashes).

  2. Adjust the URL pattern to accept optional trailing slash
    Open your polls/urls.py file, and modify the detail path pattern from:

    url(r'^(?P<question_id>[0-9]+)/$', views.detail, name='detail'),
    

    To:

    url(r'^(?P<question_id>[0-9]+)/?$', views.detail, name='detail'),
    

    The ? after the slash makes it optional, so both polls/1 and polls/1/ will now match the detail view.

  3. Verify the APPEND_SLASH setting
    Check your settings.py file for the APPEND_SLASH option. Django defaults this to True, which automatically redirects requests without trailing slashes to the version with the slash (so polls/1 would redirect to polls/1/). If you've set APPEND_SLASH = False, this redirect won't happen—just change it back to True and restart your server to fix the issue.


内容的提问来源于stack exchange,提问作者CoderMACi5

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:55:17