Django Formset management-form validation error

后端 未结 2 958
说谎
说谎 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 02:57

    I have figured this out after running into the same error. In situations where you are using a form and a formset in the same view and template, the form should be processed before the formset to avoid raising a formset validation error.

    of importance Also, if request.method == 'POST': SHOULD ONLY be used on the formset. So the above will appear as below in the view:

    @login_required
    def home(request):
    
        user = UserProfile.objects.get(pk=request.session['_auth_user_id'])
        blogz = list(blog.objects.filter(deleted='0'))
        delblog = modelformset_factory(blog, exclude=('poster','date' ,'title','content')) 
        if request.user.is_staff== True:
            staff = 1
        else:
            staff = 0
        staffis = 1
    
        blogform = BlogForm(request.POST)
        if blogform.is_valid():
            blogform.save()
            return HttpResponseRedirect('/home')
    
       else:
        blogform = BlogForm(initial = {'poster':user.id})
    
        if request.method == 'POST':
        delblogformset = delblog(request.POST)
        if delblogformset.is_valid():
            delblogformset.save()
            return HttpResponseRedirect('/home')
    
        else:
            delblogformset = delblog(queryset=blog.objects.filter( deleted='0'))
    
    
    
        blogs= zip(blogz,delblogformset.forms)  
    
        paginator = Paginator(blogs, 10) # Show 25 contacts per page
    
        # Make sure page request is an int. If not, deliver first page.
        try:
            page = int(request.GET.get('page', '1'))
        except ValueError:
            page = 1
    
        # If page request (9999) is out of range, deliver last page of results.
        try:
            blogs = paginator.page(page)
        except (EmptyPage, InvalidPage):
            blogs = paginator.page(paginator.num_pages)
    
        return render_to_response('home.html', {'user':user, 'blogform':blogform, 'staff': staff, 'staffis': staffis, 'blog':blogs, 'delblog':delblogformset}, context_instance = RequestContext( request ))
    
    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题