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变量的位置。在google上搜索,堆栈溢出和django api似乎都没有给出任何答案。

最佳答案

您应该检查下面的代码:

return render(request, 'polls/detail.html', {
    'question': p,
    'error_message': "You didn't select a choice.",
})

关于python - Django教程第4部分中定义的变量error_message在哪里,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19619458/

10-15 22:03