0

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

<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 in loop 是在运行时给出的。它不是预定义值的列表。

4

2 回答 2

1

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

例如

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

参考:小部件

于 2012-09-05T13:03:54.987 回答
0

例如

CUSTOMERTYPE = (
    (u'-', u'-'),
    (u'Single', u'Single Customer'),
    (u'Community', u'Community Change'),
)
CustomerType = forms.ChoiceField(choices=CUSTOMERTYPE)
于 2012-09-05T13:05:56.967 回答