此字段是Django使用formset时出现错误所必需的

此字段是Django使用formset时出现错误所必需的,django,django-forms,Django,Django Forms,我有三个表单,forms.py: class HotelForm(forms.Form): rooms = forms.IntegerField(label=(u'Rooms'), min_value=1) class TouristsForm(forms.Form): adult = forms.IntegerField(label=(u'Adults'), min_value=1, initial=1) children = forms.In

我有三个表单,forms.py:

class HotelForm(forms.Form):
        rooms = forms.IntegerField(label=(u'Rooms'), min_value=1)

class TouristsForm(forms.Form):
        adult = forms.IntegerField(label=(u'Adults'), min_value=1, initial=1)
        children = forms.IntegerField(label=(u'Children'), min_value=0, initial=0, required=False)

class ChildrenAgeForm(forms.Form):
        children_age = forms.IntegerField(label=(u'Children Age'), min_value=2, max_value=10, initial=2, required=False)
这就是我在views.py中实现表单集和验证的方式:

def bookingForm(request):
        TouristsFormSet = formset_factory(TouristsForm, extra = 1, max_num = 15)
        ChildrenAgeFormSet = formset_factory(ChildrenAgeForm, extra = 1, max_num = 20)
        if request.method == 'POST':
                booking_form = HotelForm(request.POST, prefix='booking_form')
                tourists_formset = TouristsFormSet(request.POST, prefix='tourists')
                childrenage_formset = ChildrenAgeFormSet(request.POST, prefix='childrenage')
                if booking_form.is_valid() and tourists_formset.is_valid() and childrenage_formset.is_valid():
                        rooms = booking_form.cleaned_data['rooms']

                        for i in range(0, tourists_formset.total_form_count()):
                                tourists_form = tourists_formset.forms[i]
                                tourists = tourists_form.cleaned_data

                        for n in range(0, childrenage_formset.total_form_count()):
                                childrenage_form = childrenage_formset.forms[n]
                                childrenage = childrenage_form.cleaned_data

                        template = get_template("booking/result.html")
                        context = Context({'tourists_formset':tourists_formset, 'childrenage_formset':childrenage_formset })
                        html = template.render(context)
                        return HttpResponse( html ) 

        else:
                booking_form = HotelForm()
                tourists_formset = TouristsFormSet(prefix='tourists')
                childrenage_formset = ChildrenAgeFormSet(prefix='childrenage')
        return render(request, 'booking/booking.html', { 'booking_form' : booking_form, 'tourists_formset' : tourists_formset, 'childrenage_formset' : childrenage_formset })
这就是我实现html文件的方式:

{{ tourists_formset.management_form }}
{% for tourists in tourists_formset %}
   {{ tourists }}
{% endfor %}
{{ childrenage_formset.management_form }}
{% for childrenage in childrenage_formset %}
   {{ childrenage }}
{% endfor %}

每次我填写表单中的所有字段时,HotelForm表单都会出现一个错误“此字段是必需的”。我不明白为什么会这样。感谢您的帮助

您在处理POST请求时使用了前缀

booking_form = HotelForm(request.POST, prefix='booking_form')
booking_form = HotelForm(prefix='booking_form')
GET请求需要使用相同的前缀

booking_form = HotelForm(request.POST, prefix='booking_form')
booking_form = HotelForm(prefix='booking_form')

在is_有效块内的for循环毫无意义。最后只得到最后一个表单中的值。