0

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)

5
  • 1
    You are using crisply forms Commented Nov 28, 2018 at 14:37
  • Yes I am using crispy forms Commented Nov 28, 2018 at 14:39
  • Does it work? Approve answer Commented Nov 28, 2018 at 15:27
  • No it doesn't, I still see the error message. But never mind, I will try and ask a different question. Maybe my code/form design is wrong. Commented Nov 28, 2018 at 15:44
  • 1
    You should show the entire traceback, because it's not clear on which line this error is produced. I don't believe this error can occur on any of the lines of code you show here. Commented Nov 28, 2018 at 16:49

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

Why would I have to declare end_date in BigForm? I just want to use it in DateForm
You have indeed declared end_date in DateForm, but according to the error that you posted in your question, you are getting a KeyError looking for end_date in BigForm. Post the entire traceback of your error message so we can see exactly where the problem is.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.