Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django ManagementForm data is missing or has been tampered with

Tags:

python

django

I keep getting the error:

[u'ManagementForm data is missing or has been tampered with']

I can't figure out why either. Here is my view:

   def CreateWorkout(request):
    WorkoutInlineFormSet = inlineformset_factory(workout,exercise)
    if request.method == "POST" : 
        formset = WorkoutInlineFormSet(request.POST)

        if formset.is_valid(): 
            formset.save(); 
    else: 
        formset = WorkoutInlineFormSet()
    return render_to_response('submit.html',{'formset': formset},context_instance=RequestContext(request))

And here is my template:

<body>
<form method="POST" action ="">
{{ formset.management_form }}
<table>
 {% for form in formset.forms %}
            {{ form }}
        {% endfor %}

        </table>
</form> 
</body>

I've read that you have to include the formset.management_form, and I have. I thought that would be an easy fix, but I haven't been able to figure out the problem.

like image 764
Lilluda 5 Avatar asked Oct 13 '12 22:10

Lilluda 5


3 Answers

I have meet this problem.

The reason is there is NO something like form-TOTAL_FORMS, form-INITIAL_FORMS and form-MAX_NUM_FORMS) in your POST data.

You should use {{ formset.as_p }}, this will render the management_form data from the formset. If you want to make the custom formset rendering, you should not forget the management_form of the formset to let POST data be with the mangement_form data.

like image 135
ryanking Avatar answered Nov 16 '22 03:11

ryanking


When you use inline formset, you need to provide the instance that the objects relate to.

# First, fetch the instance from the db
workout = code_that_fetches_instance()

if request.method == "POST" : 
    formset = WorkoutInlineFormSet(request.POST, instance=workout)
    ...
else: 
    formset = WorkoutInlineFormSet(instance=workout)

See the example in the docs on using an inline formset in a view for more information.

If workout and exercise are your models, you should follow the python convention and rename them Workout and Exercise. Lowercase workout should be the instance that all the exercises in your formset are linked to.

like image 41
Alasdair Avatar answered Nov 16 '22 01:11

Alasdair


Change this:

  formset = WorkoutInlineFormSet(request.POST)

to this:

 formset = WorkoutInlineFormSet(request.POST or None, request.FILES or None)
like image 38
fares_backway Avatar answered Nov 16 '22 01:11

fares_backway