ManagementForm data missing error while formset va

2019-05-16 18:56发布

formset creation in views.py:

ffact = formset_factory(Form,extra=somenum]))
fset = ffact(prefix='pfix')

validation in views.py:

ffact = formset_factory(Form,extra=3))
fset = ffact(request.POST) 
if fset_is.valid():
    blah blah

this is resulting in Exception Type: ValidationError at /app/index/ Exception Value: [u'ManagementForm data is missing or has been tampered with'] django-docs did mention about this. I'm not sure how to provide management data. I tried something like this ,

try:
  fset = ffact(request.POST)
except ValidationError:
  fset = None
if fset and fset.is_valid():
  blah blah

But still i get the same error.Any ideas? Thanks.

2条回答
smile是对你的礼貌
2楼-- · 2019-05-16 19:39

Is the management data in request.POST? http://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform

The following info has to be in request.POST:

data = {
    'form-TOTAL_FORMS': u'1',
    'form-INITIAL_FORMS': u'0',
    'form-MAX_NUM_FORMS': u'',
}

There is a shortcut for rendering the hidden fields: {{ my_formset.management_form }}

查看更多
干净又极端
3楼-- · 2019-05-16 19:54

rendering the formset.management_form in the template

{{fset.management_form}}

this allows the management form data available and hence data is complete.But if prefix is added while genarating formsets

adding prefix should fix that issue.

fset = ffact(request.POST,prefix='pfix')
查看更多
登录 后发表回答