Reputation: 2212
In my Django app i have three forms:
class RoomsForm(forms.Form):
rooms = forms.IntegerField(min_value=1)
class TouristsForm(forms.Form):
adult = forms.IntegerField(label=(u'Adults'), min_value=1, initial=1, widget = forms.HiddenInput)
children = forms.IntegerField(label=(u'Children'), min_value=1, required=False, widget = forms.HiddenInput)
class ChildrenAgeForm(forms.Form):
children_age = forms.IntegerField(min_value=2, max_value=10, required=False, widget = forms.HiddenInput)
in view.py i try to clean data:
def RoomsForm(request):
TouristsFormSet = formset_factory(TouristsForm)
ChildrenAgeFormSet = formset_factory(ChildrenAgeForm)
if request.method == 'POST':
rooms_form = RoomsForm(request.POST, prefix='rooms_form')
tourists_formset = TouristsFormSet(request.POST, prefix='tourists')
childrenage_formset = ChildrenAgeFormSet(request.POST, prefix='childrenage')
if rooms_form.is_valid() and tourists_formset.is_valid() or childrenage_formset.is_valid():
rooms = rooms_form.cleaned_data['rooms']
for i in range(0, tourists_formset.total_form_count()):
tourists_form = tourists_formset.forms[i]
print tourists_form.cleaned_data
but it always rise the error KeyError at /rooms/ 'rooms'. Can somebody help me with it?
Upvotes: 0
Views: 668
Reputation: 309109
In this line, you are using or
instead of and
, so you are not guaranteed that rooms_form
is valid.
if rooms_form.is_valid() and tourists_formset.is_valid() or childrenage_formset.is_valid():
When rooms_form
is invalid, room
is not in rooms_form.cleaned_data
, so you get the KeyError
.
Upvotes: 3