迭代Django Crispy Forms的MulitpleChoiceFields列表

时间:2015-06-23 20:17:42

标签: django django-forms django-crispy-forms

这是一个代码片段,它使用for循环生成3个MultipleChoiceFields的列表。请注意,每个字段都必须明确命名为django-cripsy-forms才能呈现它们,但必须有一种方法可以使用Crispy Forms迭代这些字段,所以我不必事先知道列表有多大MultipleChoiceFieds将是。任何想法??

    items = tuple(items)
    locations.append(forms.MultipleChoiceField(widget=forms.SelectMultiple, label = '', choices=items))


field1 = locations[0]
field2 = locations[1]
field3 = locations[2]

def __init__(self, *args, **kwargs):

    self.helper = FormHelper()
    self.helper.form_id = 'id-qkview-form'
    self.helper.form_show_errors = True
    self.helper.form_action = 'F5-fetch-qkview'
    self.helper.form_method = 'POST'
    self.helper.form_class = 'form-vertical'
    self.helper.label_class = 'col-lg-2'
    self.helper.field_class = 'col-lg-4'
    self.helper.add_input(Submit('submit', 'Submit'))

    self.helper.layout = Layout(
        Fieldset(
            'LOC01',
            'field1',
        ),
        Fieldset(
            'LOC02',
            'field2',
        ),
        Fieldset(
            'LOC01',
            'field3',
        ),
     )

    super(F5qkviewForm, self).__init__(*args, **kwargs )

0 个答案:

没有答案