edb500
edb500

Reputation: 323

Multiple forms in forms.py

This is a Django project.

forms.py

class BigForm(forms.Form):
    template = forms.CharField(label='Template', widget=forms.Select(choices=CHOICES))

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

        self.helper = FormHelper
        self.helper.form_method = 'post'
        self.helper.layout = Layout(
            Field('template'),
            Submit('submit', 'Submit', css_class='btn-success')
    )


class DateForm(forms.Form):
    start_date = forms.CharField(label='Start date')
    end_date = forms.CharField(label='End date')

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

        self.helper = FormHelper
        self.helper.form_method = 'post'
        self.helper.layout = Layout(
            Field('start_date', css_class='form-control'),
            Field('end_date', css_class='form-control')
        )

views.py

def myForm(request):
    main_form = BigForm()
    date_form = DateForm()
    return render(request, 'polls/main.html', {'main_form': main_form, 'date_form': date_form})

Is there something wrong with this? I keep getting KeyError: "Key 'end_date' not found in 'BigForm'. Choices are: template."

I just want two separate form classes (for two separate forms)

Upvotes: 0

Views: 63

Answers (1)

Daniel Holmes
Daniel Holmes

Reputation: 2002

You have not declared end_date as a field within BigForm. It exists in DateForm.

Upvotes: 1

Related Questions