【发布时间】:2016-06-04 18:05:55
【问题描述】:
我有一个选择字段,即使出现错误(例如,必填字段为空),它也应保持其选定值,但它与当前值不匹配。
<label for="{{ form.movie.id_for_label }}" class="col-sm-3 control-label">Movie:</label>
<select id="{{ form.movie.id_for_label }}" name="movie" class="form-control">
{% for x,y in form.fields.movie.choices %}
<option value="{{ x }}"{% if form.movie.value == x %} selected="selected"{% endif %}>{{ y }}</option>
{% endfor %}
如果我选择值 2,那么在发生错误后 form.movie.value 的值是 2,但是在你的循环中的代码中,当 x = 2 时,等式不会返回 true,所以没有选择值将被保留。
为什么会这样,问题出在哪里?
【问题讨论】:
标签: python django django-forms django-templates