Django - ManagementForm data is missing or has been tampered with
<p>I've been racking my brain over this problem for the past few days and I've read numerous other questions regarding the same error but they all seem to be different cases (not including management form, forgetting to update TOTAL_FORMS, etc etc) and do not resolve my problem. I have a page which could contain multiple formsets in a single HTML form. When I am posting the data back to the server, it fails on the is_valid() check for the formsets with the error in the title. I am new to web development and Django so please forgive me if I made a silly mistake or am taking an approach that will not work. </p>

<pre><code>def purchase(request):
    return generic_form_view(request, "inventory_tracking/add_purchases.html",
                                  "Successfully added purchases for %s.",
                                  PurchaseForm,
                                  [formset_factory(PurchaseForm.LiquorForm),
                                  formset_factory(PurchaseForm.NonLiquorForm)])

def generic_form_view(request, template, success_message, ParentForm, FormSets):
    if request.method == 'POST':
    request_params = copy(request.POST)
    parent_form = ParentForm(request_params)
    formsets = list(map(lambda form_set: form_set(request_params), FormSets))
    if parent_form.is_valid():  # This works.
        for formset in formsets:
            if formset.is_valid():  # Fails here.
</code></pre>

<p>Here is a snippet from my template:</p>

<pre><code> &lt;form action="{% block form_action %}{% endblock %}" method="post"&gt;
    {% csrf_token %}
    &lt;div class="row"&gt;
        &lt;div class="row"&gt;
            &lt;div class=" well well-lg"&gt;
                &lt;div class="row"&gt;
                {{ parent_form.management_form }}
                {% for field in parent_form %}
                    &lt;div class="col-lg-6"&gt;
                        &lt;div class="form-group"&gt;
                            &lt;label class="control-label"&gt;{{ field.label }}&lt;/label&gt;
                            {{ field }}
                        &lt;/div&gt;
                    &lt;/div&gt;
                {% endfor %}
                &lt;/div&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class="row"&gt;
        {% for formset in formsets %}
            {{ formset.management_form }}
            &lt;div class="row"&gt;
                &lt;div class="well well-lg"&gt;
                    {% for form in formset %}
                        &lt;div id="{{ form.prefix }}" class="row"&gt;
                        ...
</code></pre>

<p>I've been trying to debug this and I noticed something a little interesting but since I am not too familiar with Django it could be a red herring. In the POST, I see the management_form data for the formsets I am creating but I do not see the management_form data for the parent formset (in this case PurchaseForm). However the parent_form is passing validation and the other formsets are not. </p>

<p>I expected this to be a silly problem and I turned about to be right! When my generic_form_view method creates the formsets on the GET request I was adding a prefix like the documentation mentioned but I was not adding a prefix when creating the formsets on the POST. </p>

python django django-forms