首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Django Page not found (404) polls:详细url问题

Django Page not found (404) polls:详细url问题
EN

Stack Overflow用户
提问于 2019-04-17 00:23:30
回答 1查看 64关注 0票数 0

在Django教程中,它要求我们将视图调整为通用视图。发布后,我可以在index.html上点击我的问题"What's up“,但它导致了一个页面未找到错误。代码如下:

Urls.py(用于投票)

代码语言:javascript
复制
from django.urls import path

from . import views

app_name='polls'

urlpatterns= [ 
    #ex: /polls/
    path('',views.IndexView.as_view(),name='index'),
    #ex: /polls/5/
    path('<int:pk>/',views.DetailView.as_view(),name='detail'),
    #ex: /polls/5/results/
    path('<int:pk>/results/',views.ResultsView.as_view(),name='results'),
    #ex: /polls/5/vote/
    path('<int:question_id>/vote/',views.vote,name='vote'),
]

index.html

代码语言:javascript
复制
{% load static %}

<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_text }}</a></li>
    {% endfor %}
    </ul>
{% else %}
    <p>No polls are available.</p>
{% endif %}

views.py

代码语言:javascript
复制
from django.shortcuts import render,get_object_or_404
from django.http import Http404,HttpResponseRedirect
from django.urls import reverse
from django.views import generic


# Create your views here.
#from django.http import HttpResponse
from django.template import loader
from .models import Question, Choice

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'

当我点击链接"What's up?“在index.html中,它将我引向以下错误:

代码语言:javascript
复制
Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/polls/%7B%25%20url%20'polls:detail'%20question.id%20%25
Using the URLconf defined in mysite.urls, Django tried these URL patterns, in this order:

polls/ [name='index']
polls/ <int:pk>/ [name='detail']
polls/ <int:pk>/results/ [name='results']
polls/ <int:question_id>/vote/ [name='vote']
admin/
The current path, polls/{% url 'polls:detail' question.id %, didn't match any of these.

You're seeing this error because you have DEBUG = True in your Django settings file. Change that to False, and Django will display a standard 404 page.

我来自瓶子背景,学习Django,除了这个小问题之外,我发现它真的组织得很好。

我已尝试将question.id更改为question.pk。此外,我还尝试在“polls:detail”前后更改引号,但也无济于事。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-17 00:32:07

您在index.html中的{% url %}标记末尾缺少尾随的}。它应该是:

代码语言:javascript
复制
 <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55712669

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档