Iterating a list of MulitpleChoiceFields for Django Crispy Forms

277 views Asked by At

Here's a a code snippet that used a for loop to generate a list of 3 MultipleChoiceFields. Notice, that each field has to explicitly named for django-cripsy-forms to render them, but there has got to be a way to iterate through these with Crispy Forms so I don't have to know in advance how big the list of MultipleChoiceFieds will be. Any ideas??

    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

There are 0 answers