Django显示多个选择模板中的选定项目

时间:2013-02-27 22:37:25

标签: django django-forms django-templates

我在ModelForm中有多个选择。当页面重新加载并出现错误时,我希望显示当前选定的项目。

view.html

error: {{ form.recipients.errors }}<br/>
currently selected: <br/>
{% for recipient in form.recipients %} <!--I want some type of loop through only the selected items in my recipients -->
    {{ recipient }}<br/>
{% endfor %}
{{ form.recipients }}

1 个答案:

答案 0 :(得分:1)

您希望循环字段的值,而不是字段本身:

{% for recipient in form.recipients.value %}
    {{ recipient }}
{% endfor %}