首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >NoReverseMatch at - django polls应用教程

NoReverseMatch at - django polls应用教程
EN

Stack Overflow用户
提问于 2020-07-18 23:46:14
回答 1查看 61关注 0票数 0

当我试图加载一个页面时,我一直得到这样的结果:

代码语言:javascript
复制
NoReverseMatch at /polls/
Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']

In template C:\Users\sarah\Desktop\django2\myproject\my_site\polls\templates\polls\index.html, error at line 20

第20行:

代码语言:javascript
复制
<form action="{% url 'polls:vote' question.id %}" method="post">

我是一个完全的初学者在django,css,html,...我一直在查看教程,将我的代码与教程中的代码参考进行比较,但我没有发现任何错误。

我的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 %}

<h1>{{ question.question_text }}</h1>

{% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}

<form action="{% url 'polls:vote' question.id %}" method="post">
{% csrf_token %}
{% for choice in question.choice_set.all %}
    <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
    <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
{% endfor %}
<input type="submit" value="Vote">
</form>

Views.py中的投票函数:

代码语言:javascript
复制
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):
        # Redisplay the question voting form.
        return render(request, 'polls/detail.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,)))

urls.py:

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


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

谢谢。

EN

回答 1

Stack Overflow用户

发布于 2020-07-18 23:58:24

这里的主要原因是您将index.html与表单一起使用,您应该在polls/detail中使用该表单,这样您就可以通过pk访问question对象。现在,您正在尝试访问question的id属性,但question在index.html中未定义。

在polls/details.html中放置以下内容

代码语言:javascript
复制
<h1>{{ question.question_text }}</h1>
<form action="{% url 'polls:vote' question.id %}" method="post">
    {% csrf_token %}
    {% for choice in question.choice_set.all %}
        <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
        <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
    {% endfor %}
    <input type="submit" value="Vote">
</form>

然后,导航到polls/1或您想要的任何question.id,然后尝试使用表单。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62970545

复制
相关文章

相似问题

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