Django:找不到页面(404)

时间:2015-07-30 18:50:20

标签: python django views django-urls

所以我正在使用版本1.8

浏览Django教程

当我转到应显示索引的页面时,该页面位于

http://127.0.0.1:8000/

我收到404错误,上面写着

Page not found (404)
  Request Method:   GET
    Request URL:    http://127.0.0.1:8000/

Using the URLconf defined in forumtest.urls, Django tried these URL patterns, in this order:
^polls/
^admin/
The current URL, , didn't match any of these.

这些是我的网页。

forumtest / urls.py:

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


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

我也正在创建一个名为polls的应用程序,它有自己的urls.py

polls / urls.py:

from django.conf.urls import url

from . import views

urlpatterns = [
    # ex: /polls/
    url(r'^$', views.IndexView.as_view(), name='index'),
    # ex: /polls/5/
    url(r'^(?P<pk>[0-9]+)/$', views.DetailView.as_view(), name='detail'),
    # ex: /polls/5/results/
    url(r'^(?P<pk>[0-9]+)/results/$', views.ResultsView.as_view(),  name='results'),
    # ex: /polls/5/vote/
    url(r'^(?P<question_id>[0-9]+)/vote/$', views.vote, name='vote'),
]

这是我的观点页面:

polls / views.py:

from django.shortcuts import render,get_object_or_404
from django.http import HttpResponseRedirect,HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic

from .models import Choice,Question

# Create your views here.
class IndexView(generic.ListView):
    template_name = 'polls/index.html'
    context_object_name = 'latest_question_list'

    def get_queryset(self):
        """Return the last five published questions"""
        return Question.objects.order_by('-pub_date')[:5]

class DetailView(generic.DetailView):
    model = Question
    template_name = 'polls/detail.html'

class ResultsView(generic.DetailView):
    model = Question
    template_name = 'polls/results.html'

def vote(request,question_id):
    p = get_object_or_404(Question, pk=question_id)
    try:
        selected_choice = p.choice_set.get(pk=request.POST['choice'])
    except(KeyError,Choice.DoesNotExist):
        #redisplay the question voting form
        return render(request,'polls/detail.html',{
            'question':p,
            'error_message': "you didn't select a choice.",
        })
    else:
        selected_choice.votes += 1
        selected_choice.save()
        # Always return an HttpResponseRedirect after successfully dealing
        # with POST data. This prevents data from being posted twice if a
        # user hits the Back button.
        return HttpResponseRedirect(reverse('polls:results', args=(p.id,)))

我不知道如何修复这样的问题,所以如果有人可以提供帮助那就太棒了。

感谢。

1 个答案:

答案 0 :(得分:2)

错误消息显示错误。您已在/ polls /下定义了网址,因此这是您应该访问的地址。

相关问题