Django教程 - 查看详细信息时的轮询错误404

时间:2015-09-17 15:47:39

标签: python django django-urls

我关注https://docs.djangoproject.com/en/1.8/intro/tutorial03/ 我已经按照教程中的方式配置了我的项目目录。 我正在为民意调查配置网址。根据教程我定义了详细信息,结果,在投票的urls.py中的views.py和poll url中投票,这是在项目目录的urls.py中定义的。 当我访问

本地主机:8000 /轮询/

它有效。 但是,当我试图访问一个民意调查的详细信息时,它会抛出404.I尝试了我的实际问题ID,但都是徒劳的。
在Docs中他们说“at”/ polls / 34 /“。它将运行detail()方法” 但对我来说它会抛出404

本地主机:8000 /轮询/ 34 /

Using the URLconf defined in DjangoFirst.urls, Django tried these URL patterns, in this order:
^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]+)/votes/$ [name='vote']
^admin/
The current URL, polls/34/, didn't match any of these.

这是我的urls.py,位于ProjectName / ProjectName / urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    url (r'^polls', include('polls.urls')),
    url(r'^admin/', include(admin.site.urls)),

]

这是我的民意调查urls.py位于ProjectName / polls / urls.py

from django.conf.urls import url

from . import views

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

这是我的民意调查视图

from django.http import HttpResponse


def index(request):
    return HttpResponse("Hello World")

def detail(request, question_id):
    return HttpResponse("You are looking at question %s." % question_id)

def results(request,  question_id):
    response =  "You are looking at response of question %s."
    return HttpResponse(response % question_id)

def vote(request, question_id):
    return HttpResponse("You are voting on question %s." % question_id)

我找不到我的代码有什么问题。请帮帮我?

2 个答案:

答案 0 :(得分:7)

我认为它应该是(在你的代码中它缺少url(r'^polls/', include('polls.urls')), ):

url (r'^polls', include('polls.urls')),

而不是:

inspect

希望这有帮助!检查tutorial page:)

答案 1 :(得分:0)

对我来说,以下方法效果很好

path(r'projectapp',include('projectapp.urls')),