找不到页(404)请求方法:GET Request URL:http://127.0.0.1:8000 /

时间:2016-06-28 11:36:15

标签: django django-templates django-views django-urls django-settings

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

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的/轮询/ 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的/轮询/ 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>

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

我的管理员工作正常!

我是Django的新手并且一直在寻找并询问并暂时坚持了一段时间。

2 个答案:

答案 0 :(得分:1)

您在http://127.0.0.1:8000/上获得404,因为您尚未为该网址创建任何网址格式。您已包含网址http://127.0.0.1:8000/polls/,因为您已将民意调查网址包含在

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

空的子弹表示您的polls/index.html模板存在问题。看起来你有一个拼写错误并放置{{ question.question_test }}而不是{{ question.question_text }}。确保它与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 %}

答案 1 :(得分:0)

确保您的代码中没有拼写错误。在引号之间放置空格也会导致此错误。只要确保你输入了 path('',include('home.urls')) 而不是 path(' ',include('home.urls'))

注意:这里的 home.urls 是我在 Django 中的应用程序的名称