Django多输入表单

时间:2012-09-05 12:37:14

标签: python django django-forms

我有一个看起来像这样的模板:

<form method="post" action="{% url ... %}">
{% csrf_token %}
<table class="table">
    {% for x in X %}
        <tr>
            <td>{{ x.name }}</td>
            <td><input type="checkbox" value="{{x.id}}"" /></td>
        </tr>
    {% endfor %}
</table>
</form>

我应该在django表单类中使用哪种类型的字段来选中复选框?

感谢您的回答

编辑:

我没有提及,循环中的X是在运行时给出的。它不是预定义值的列表。

2 个答案:

答案 0 :(得分:1)

您需要为表单字段指定CheckBoxMultipleSelect小部件。

E.g。

def MyForm(forms.Form):
    favorite_colors = forms.MultipleChoiceField(required=False,
        widget=CheckboxSelectMultiple, choices=FAVORITE_COLORS_CHOICES)
    ...

参考:Widgets

答案 1 :(得分:0)

例如

CUSTOMERTYPE = (
    (u'-', u'-'),
    (u'Single', u'Single Customer'),
    (u'Community', u'Community Change'),
)
CustomerType = forms.ChoiceField(choices=CUSTOMERTYPE)
相关问题