当我将request.POST
参数传递给我的CargoUnitFormSet
时。它在命令行中出现此错误。
File "C:\Python27\lib\site-packages\django\utils\functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Python27\lib\site-packages\django\forms\formsets.py", line 98, in management_form
code='missing_management_form',
ValidationError: [u'ManagementForm data is missing or has been tampered with']
在myform中我创建了一个formset。我的formset显示在我的网页上。每当我传递request.POST
作为参数来保存我的formset时,它都会给出此验证错误。这是我的视图功能:
def create_entity(request):
from django.forms import BaseFormSet
from django.forms import formset_factory
from django.forms.models import modelformset_factory, BaseModelFormSet
CargoUnitFormSet = formset_factory(CargoUnitForm,max_num=3)
data = dict()
if request.method == 'POST':
cargo_form = CargoForm(request.POST, request.FILES or None)
cargo_unit_formset = CargoUnitFormSet(request.POST,prefix="foo")
if not cargo_form.is_valid():
data['form_is_valid'] = False
if cargo_form.is_valid() and cargo_unit_formset.is_valid():
cargo = cargo_form.save()
for form in cargo_unit_formset.forms:
cargo_unit = form.save(commit=False)
cargo_unit.list = cargo
cargo_unit.save()
data['form_is_valid'] = True
context = {
'form': cargo_form,
'formset':cargo_unit_formset
}
data['html_form'] = render_to_string("cargos/form-create.html", context, request=request)
return JsonResponse(data)
这是我用于formset的相关html模板
<div class="row">
{{formset.management_formset}}
{% for form in formset.forms %}
<div class="item well">
{{ form.as_p }}
<p><a class="delete btn btn-danger" href="#"><i class="icon-trash icon-white"></i> Delete</a></p>
</div>
{% endfor %}
</div>
提前致谢。