Django Formset management-form validation error

后端 未结 2 973
说谎
说谎 2021-01-12 02:33

I have a form and a formset on my template. The problem is that the formset is throwing validation error claiming that the management form is \"missing or has been tampered

2条回答
  •  既然无缘
    2021-01-12 03:01

    To avoid this error just wrap your formset POST bounding in a try/except block like so.

    from django.core.exceptions import ValidationError # add this to your imports
    
    if request.method == 'POST':
       try:
          delblogformset = delblog(request.POST)
       except ValidationError:
          delblogformset = None
       if delblogformset and delblogformset.is_valid():
          delblogformset.save()
              return HttpResponseRedirect('/home')
    

    The POST request from your blogform lacks the 'ManagementForm' hidden input that is required for your delblogformset and hence the validation error is thrown. We wrap in a try/except block because we know that if ValidationError has been raised than the POST was meant for your blogform and not delblogformset.

    For more information see django docs: http://docs.djangoproject.com/en/dev/topics/forms/formsets/#understanding-the-managementform

提交回复
热议问题