【问题标题】:NoReverseMatch at /polls/ Reverse for 'vote' with arguments '('',)' not found. 1 pattern(s) tried: ['polls/(?P<question_id>[0-9]+)/vote/$']NoReverseMatch at /polls/ 未找到带有参数 '('',)' 的“投票”的反向匹配。尝试了 1 种模式:['polls/(?P<question_id>[0-9]+)/vote/$']
【发布时间】:2019-05-10 14:21:56
【问题描述】:

NoReverseMatch 在 /polls/

没有找到带有参数“(”,)”的“投票”。尝试了 1 种模式:['polls/(?P[0-9]+)/vote/$']

index.html:

    {% if latest_question_list %}
    <ul>
    {% for question in latest_question_list %}
    <!-- # the 'name' value as called by the  url  template tag -->
        <li><a href="{% url 'polls:detail' question.id %}">{{ question.question_text }}</a></li>
<!-- or:
    <li><a href=" url 'detail' question.id "> question.question_text </a></li>
    How does one make it so that Django knows which app view to create for a url when using the  url  template tag?
    So we use polls:detail
 -->
    {% 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>

enter image description hereenter image description here

下面是控制台错误。 stackoverflow 中的其他相关问题的答案如下: 不是question_id!这是question.id!

第 123 行错误 使用未找到参数“(”,)”的“投票”反向。尝试了 1 种模式:['polls/(?P[0-9]+)/vote/$']:

113         {% endfor %}
114         </ul>
115         {% else %}
116             <p>No polls are available.</p>
117         {% endif %}
118     
119         <h1>{{ question.question_text }}</h1>
120     
121         {% if error_message %}<p><strong>{{ error_message }}</strong></p>{% endif %}
122     
123         <form action="{% url 'polls:vote' question.id %}" method="post">
124         {% csrf_token %}
125         {% for choice in question.choice_set.all %}
126             <input type="radio" name="choice" id="choice{{ forloop.counter }}" value="{{ choice.id }}">
127             <label for="choice{{ forloop.counter }}">{{ choice.choice_text }}</label><br>
128         {% endfor %}
129         <input type="submit" value="Vote">
130         </form>
131         </content>

【问题讨论】:

    标签: django django-models django-forms django-templates django-views


    【解决方案1】:

    在模板的那个位置,您还没有一个名为 question 的变量。它只存在于您的 for 循环中,但错误发生在该循环结束后的表单标记中。

    url 标签是唯一真正会显示错误的标签,因为它需要使用 question.id 的值来创建 URL;但实际上该变量的所有其他用途,例如question.question_text,也会显示为空白。

    我不太明白你为什么要这样构造你的模板,但我怀疑从h1 开始的所有内容都应该更高,在endfor 标记之前。

    【讨论】:

    • 如何声明这个问题变量
    • 我刚刚告诉过你,它是在 for 循环中定义的。所以将所有使用它的代码移到循环中。
    • 它工作我只需要将表单标签隔离到 details.html
    【解决方案2】:

    更改列表视图 class DetailView(generic.ListView):class DetailView(generic.DetailView):

    这可能有效

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 1970-01-01
      • 2023-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多