django - NoReverseMatch at/polls/Reverse for 'vote' with argument '(' ', )' not found. 1 pattern(s) tried: [' polls/(?P<question_id>[0-9]+)/vote/$']

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

/polls/上没有ReverseMatch

未找到参数 '('',)' 的反向“投票”。尝试了 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 here enter image description here

下面是控制台错误。 stackoverflow 中其他相关问题的答案如下: 不是question_id!这是问题.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>

最佳答案

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

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

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

关于django - NoReverseMatch at/polls/Reverse for 'vote' with argument '(' ', )' not found. 1 pattern(s) tried: [' polls/(?P<question_id>[0-9]+)/vote/$'],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53692884/

相关文章:

python - 无法使用 pidfile 选项运行 Django 的 FastCGI

django - post_syncdb 上不存在 ContentType 匹配查询

javascript - 在 django 中比较日期并验证]

python - 自定义 django 注释

django - 在 Django Shell 中获取模型的所有字段

mysql - 如何在 Django 中创建映射表?

python - 如何计算 Django 模型中产品的总价?

django - 如何通过一些计算从不同的 ModelForm 字段中填充模型字段值?

django - 在 Django Forms 中应该测试什么?

django - 使用FormView django在form_valid中渲染模板而不是success_url