Page not found (404) Request Method: GET Request U

2019-03-05 07:20发布

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

ROOT_URLCONF = 'mysite.urls'

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

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

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

<!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>

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!

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

1条回答
孤傲高冷的网名
2楼-- · 2019-03-05 08:09

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')),

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 %}
查看更多
登录 后发表回答