在我的Django應用程序,我有三種形式:在view.pyDjango的cleaned_data KeyError異常
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)
我試圖清理數據:
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
但它總是上升的錯誤KeyError異常在/間/ 「房間」。 有人可以幫我嗎?
你的'if'邏輯的最後一部分是'or'嗎?在我看來,這需要是一個'和'。 –