5

django 教程第 4 部分具有以下代码:

{{ poll.question }}

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

<form action="{% url 'polls:vote' poll.id %}" method="post">
{% csrf_token %}
{% for choice in poll.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>

我无法找出 if 条件语句中 error_message 变量的定义位置。谷歌搜索,堆栈溢出和 django apis 似乎没有给出任何答案。

4

1 回答 1

6

你应该检查它下面的代码:

return render(request, 'polls/detail.html', {
    'question': p,
    'error_message': "You didn't select a choice.",
})
于 2013-10-27T15:05:03.233 回答