我正在使用带有CheckboxSelectMultiple小部件的choiceField。 默认情况下是否可以将所有复选框渲染为已选中状态? 谢谢!
答案 0 :(得分:15)
只需从字段的选项中设置初始值,如下所示:
MY_CHOICES = (
("some", "Some choice"),
("another", "Another choice"),
("best", "Best choice")
)
...
multiple_choice = forms.MultipleChoiceField(
label=u"Select multiple",
choices=MY_CHOICES,
widget=forms.widgets.CheckboxSelectMultiple,
initial=(c[0] for c in MY_CHOICES)
)
答案 1 :(得分:12)
我正在使用此
在表单上完成此操作class MyForm(forms.Form):
photo_list = forms.MultipleChoiceField(
label="Photos",
required=False,
help_text="Unselect the photos you want to delete",
choices=(),
widget=forms.CheckboxSelectMultiple(attrs={"checked":""})
)