找不到页面(404)请求方法:GET请求URL:http://127.0.0.1:8000/ [英] Page not found (404) Request Method: GET Request URL: http://127.0.0.1:8000/

查看:126
本文介绍了找不到页面(404)请求方法:GET请求URL:http://127.0.0.1:8000/的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在遵循django文档并制作一个简单的民意调查应用程序。我遇到以下错误:

I was following the django documentation and making a simple poll app. I have come across the following error :

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

settings.py

settings.py

ROOT_URLCONF = 'mysite.urls'

mysite / mysite / urls.py

mysite/mysite/urls.py

from django.conf.urls import include,url
from django.contrib import admin
urlpatterns = [
    url(r'^polls/',include('polls.urls')),
    url(r'^admin/', admin.site.urls),]

mysite / polls / urls.py

mysite/polls/urls.py

from django.conf.urls import url

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

mysite / polls / views.py

mysite/polls/views.py

from django.shortcuts import get_object_or_404,render
from django.http import HttpResponseRedirect, HttpResponse
from django.core.urlresolvers import reverse
from django.views import generic
from django.utils import timezone
from django.template import loader
from .models import Choice,Question
from django.template.loader import get_template
#def index(request):
#   return HttpResponse("Hello, world. You're at the polls index")
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.filter(pub_date__lte=timezone.now()).order_by('-pub_date')[5:]


class DetailView(generic.DetailView):
    model=Question
    template_name='polls/detail.html'
    def get_queryset(self):
        """
        Excludes any questions that aren't published yet.
        """
        return Question.objects.filter(pub_date__lte=timezone.now())
class ResultsView(generic.DetailView):
    model= Question
    template_name ='polls/results.html'

def vote(request, question_id):
    question=get_object_or_404(Question, pk=question_id)
    try:
        selected_choice= question.choice_set.get(pk=request.POST['choice'])
    except (KeyError, Choice.DoesNotExist):
        return render(request, 'polls/details.html',
            {
            'question':question,
            'error_message' : "You didn't select a choice" ,

            })  
    else:
        selected_choice.votes+=1
        selected_choice.save()
        return HttpResponseRedirect(reverse('polls:results', args=(question.id,)))

index.html

index.html

<!DOCTYPE HTML >
{% load staticfiles %}
<html>
<body>
<link rel="stylesheet" type="text/css" href="{% static 'polls/style.css' %}" />
{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="{% url 'polls:detail' question.id %}">{{question.question_test }}
    </a></li>
        {% endfor %}
        </ul>
    {% else %}
        <p>No polls are available.</p>
    {% endif %}
</body>
</html>

此链接 http://127.0.0.1:8000/polls/ 显示了一个包含3个项目符号的空白页面。 (我的数据库中有3个问题,它们的ID为5,6,7,因为我一直在删除和添加问题。)

This link http://127.0.0.1:8000/polls/ shows a blank page with 3 bullets. (I have 3 questions in my database and their id's are 5,6,7 because I have been deleting and adding the questions.)

我的管理员工作正常!

My admin works fine!

我是Django的新手,一直在搜索和询问,现在已经停留了一段时间。

I'm new to Django and have been searching and asking around and have been stuck on it for a while now.

推荐答案

您在 http://127.0.0.1:8000/ ,因为您尚未为该URL创建任何URL模式。您包括了URL http://127.0.0.1:8000/polls/ ,因为您已经将民意调查URL包括在

You get the 404 on http://127.0.0.1:8000/ because you have not created any URL patterns for that url. You have included the url http://127.0.0.1:8000/polls/, because you have included the polls urls with

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

空项目符号表明您的 polls / index.html 模板存在问题。看来您有错字,并已将 {{question.question_test}} 而不是 {{question.question_text}} 。确保它与教程3

The empty bullets suggest that there is a problem with your polls/index.html template. It looks like you have a typo and have put {{ question.question_test }} instead of {{ question.question_text }}. Make sure that it exactly matches the template from the tutorial 3:

{% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
        <li><a href="/polls/{{ question.id }}/">{{ question.question_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

这篇关于找不到页面(404)请求方法:GET请求URL:http://127.0.0.1:8000/的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆