Django formset u'ManagementForm 数据丢失或已被篡改'

Django formset u'ManagementForm data is missing or has been tampered with'

在我的 Django 项目中,我在一个视图上使用了 3 个表单。其中 2 个表单是表单集。我的表格看起来很像:

class HotelForm(forms.Form):
        country = forms.CharField(label=(u'Select Country'))
        city = forms.CharField(label=(u'Select City'))
        hotel = forms.ChoiceField(label=(u'Hotel Stars'))
        from_date = forms.CharField(label=(u'Date from'))
        to_date = forms.CharField(label=(u'Date to'))
        rooms = forms.IntegerField(label=(u'Rooms'), min_value=1)
        food = forms.ChoiceField(label=(u'Food'),
                widget = forms.Select, choices = FOOD_CHOICES)

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

class ChildrenAgeForm(forms.Form):
        children_age = forms.IntegerField(min_value=2, max_value=10, initial=2, required=False)

根据 HotelForm 字段的数量 rooms 我使用 js 添加表单集 TouristsFormSet:

$('#id_booking_form-rooms').on('change', function(e){
   var n = $('#id_booking_form-rooms').val() || 0;
   var html = "";

   for (var i = 0; i < n; i++) {
      html += "<input id='id_tourists-TOTAL_FORMS' type='hidden' value='1' name='tourists-TOTAL_FORMS'>"
      + "<input id='id_tourists-INITIAL_FORMS' type='hidden' name='tourists-INITIAL_FORMS'>"
      + "<input id='id_tourists-MIN_NUM_FORMS' type='hidden' name='tourists-MIN_NUM_FORMS'>"
      + "<input id='id_tourists-MAX_NUM_FORMS' type='hidden' name='tourists-MAX_NUM_FORMS'>"
      + "<div>Quantity of people in the room " + (i + 1) + "</div>"
      + "<br/><label for='id_tourists-" + i + "-adult'>Adults quantity:</label>"
      + "<input id='id_tourists-" + i + "-adult' type='number' name='tourists-" + i + "-adult' value='0'/>"
      + "<label for='id_tourists-" + i + "-children'>Children quantity:</label>"
      + "<input id='id_tourists-" + i + "-children' type='number' name='tourists-" + i + "-children' class='children_age' value='0'/>"
      + "<div class='extrafieldWrapperChAge'></div>";
      }
      $(".extrafieldWrapper").html(html);
});

根据表单集 TouristsFormSetchildren 的数量,我使用 js 添加表单集 ChildrenAgeFormSet:

$(".extrafieldWrapper").on('change', '.children_age', function(e){
   var n = $(this).val() || 0;
   var html = "";
   for (var i = 0; i < n; i++) {
      html += "<input id='id_childrenage-TOTAL_FORMS' type='hidden' value='1' name='childrenage-TOTAL_FORMS'>"
      + "<input id='id_childrenage-INITIAL_FORMS' type='hidden' name='childrenage-INITIAL_FORMS'>"
      + "<input id='id_childrenage-MIN_NUM_FORMS' type='hidden' name='childrenage-MIN_NUM_FORMS'>"
      + "<input id='id_childrenage-MAX_NUM_FORMS' type='hidden' name='childrenage-MAX_NUM_FORMS'>"
      + "<br/><label for='id_childrenage-" + i + "-children_age'>Children Age "+(i+1)+"</label>"
      + "<input id='id_childrenage-" + i + "-children_age' type='number' value='0' name='childrenage-" + i + "children_age' />";
      }
$(this).next('.extrafieldWrapperChAge').html(html);
});

我的 html 文件看起来很像:

<div class="large-6 columns">
   <div class="fieldWrapper">
     {% if booking_form.city.errors %}
       <ol style="list-style-type:square" >
         {% for error in booking_form.city.errors %}
           <li><strong>This field is required</strong></li>
         {% endfor %}
        </ol>
      {% endif %}
      {{ booking_form.city.label_tag }}
      {{ booking_form.city }}
     </div>
 </div>
 <!-- and other HotelForm fields -->
 <div class="extrafieldWrapper">
 <!-- here is load formset fields -->
 </div>
 <div class="row">
   <input type="submit" value="Find">
 </div>

看起来像我的观点:

def post(self, request, *args, **kwargs):
  TouristsFormSet = formset_factory(TouristsForm, extra = 1, max_num = 15)
  ChildrenAgeFormSet = formset_factory(ChildrenAgeForm, extra = 1, max_num = 20)
  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():
    ...

一切正常,但如果我保留 adultTouristsFormSet and/or children age 字段 ChildrenAgeFormSet 的默认字段值,我收到错误 ValidationError at /booking/、[u'ManagementForm 数据丢失或已被篡改']。

我尝试比较浏览器中的 html 代码,当我使用 js 和不使用 js 时只使用 {{tourists_formset}}{{childrenage_formset}}。在我看来它是 same 。当我使用没有 js 的表单集时,一切正常

同样在回溯中,当字段子项的值为 0 且 _('ManagementForm data is missing or has been tampered with'), code='missing_management_form',

时,我发现这一行 return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)

如果有人能帮助我,我将不胜感激。

应该为每个表单集添加一次管理表单。在您的 JavaScript 中,您似乎为每个表单添加一次。