我有一个包含get和post方法的类视图。在get方法中,我有以下这些项目:
items = Item.objects.filter(user = the_user)
item_pricing_forms = [] # list of forms
item_component_formsets = [] # list of inline formsets
for item in items:
item_pricing_form = ItemPricingForm()
item_pricing_forms.append(item_pricing_form)
item_component_formset = inlineformset_factory(Item, Component, form = ComponentForm, can_delete = False, extra = item.num_of_components)
item_component_formsets.append(item_component_formset)
然后,我将它们压缩成一个变量
zipped_item_pricing_components = zip(items, item_pricing_forms, item_component_formsets)
在我的模板中:
<form action = "" method = "post">
{% csrf_token %}
<table>
{% for item, item_pricing_form, item_components_formset in zipped_item_pricing_components %}
<tr><th>Item {{item}}</th></tr>
{{ item_pricing_form }}
{{ item_components_formsets.management_form }}
{% for item_components_formset in item_components_formsets }}
{{item_components_formset }}
{% endfor %}
{% endfor %}
<tr><td>
<input type="submit" value="Submit">
</td></tr>
</table>
</form>
这在get请求中呈现得很好。
但是,当传递post请求时,我只能看到每个列表的最后一个元素。因此,如果我在POST视图的开头某处断言False,则POST数据具有每个列表的最后一个元素的信息(例如,item_pricing_forms的最后一个元素和item_component_formsets的最后一个元素。为什么Django不返回完整清单?我做错了什么?