在我的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)
根据rooms
的字段HotelForm
的数量,我使用js添加formset 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);
});
根据formset children
的{{1}}的数量,我使用js添加formset TouristsFormSet
:
ChildrenAgeFormSet
看起来像我的html文件:
$(".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);
});
看起来像我的观点:
<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():
...
,adult
和/或children
TouristsFormSet
字段children age
字段,我会收到错误ChildrenAgeFormSet
,[u&#39; ManagementForm数据丢失或已被篡改&#39;]。
当我使用js而没有使用ValidationError at /booking/
和{{tourists_formset}}
的js时,我尝试比较浏览器中的html代码。在我看来它是same。当我使用没有js的formset时一切正常
同样在追溯中,当字段子项的值为0且{{childrenage_formset}}
return min(self.management_form.cleaned_data[TOTAL_FORM_COUNT], self.absolute_max)
如果有人帮助我,我将感激不尽。
答案 0 :(得分:1)
每个表单集应添加一次管理表单。在您的JavaScript中,看起来好像是为每个表单添加一次。