gpt4 book ai didi

python - Django:找不到页面(404)

转载 作者:行者123 更新时间:2023-12-01 04:35:51 24 4
gpt4 key购买 nike

所以我正在学习 Django 教程,使用版本 1.8

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

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.

这些是我的网址页面。

论坛测试/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'),
]

这是我的观点页面:

民意调查/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,)))

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

谢谢。

最佳答案

错误消息告诉您出了什么问题。您已在/polls/下定义了 url,因此这就是您应该访问的地址。

关于python - Django:找不到页面(404),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31731794/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com