NoReverseMatch at / polls /
使用参数'('',)找不到“投票” . 尝试过1种模式:['民意调查/(?P [0-9])/投票/ $']
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中的其他相关问题的答案如下:not question_id!这是个问题 .
error at line 123 反向'vote',找不到参数'(' ',)' . 尝试过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>
1 回答
你没有在模板的那个点上得到一个名为
question
的变量 . 它只存在于for循环中,但错误发生在form循环结束后的form标记中 .url
标签是唯一一个实际显示错误的标签,因为它需要使用question.id
的值来创建URL;但实际上变量的所有其他用途,例如question.question_text
,也会显示为空白 .我没有像这样构建你的模板,但我怀疑
h1
之前的所有内容应该高得多,在endfor
标签之前 .