Search code examples
djangoformset

Management Formset error is getting triggered


The HTML code below is returning a ['ManagementForm data is missing or has been tampered with']. This HTML was what gave me the custom look I was going for, but why is this happening? I don't understand, since I have declared the management_data tag.

HTML

<form method="POST" enctype="multipart/form-data" action=".">
{{ formset.management_data }}

<!-- Security token -->
{% csrf_token %}

{{ formset.non_form_errors.as_ul }}
<table>
    {% for form in formset.forms %}
        {% if forloop.first %}
        <thead>
            <tr>
                 {% for field in form.visible_fields %}
                     <th name={{field.label}}>{{ field.label }}</th>
                 {% endfor %}
            </tr>
            </thead>
            {% endif %}
            <tr class="{% cycle row1 row2 %}">
                {% for field in form.visible_fields %}
                    <td name={{field.label}}>
                        {# Include the hidden fields in the form #}
                        {% if forloop.first %}
                        {% for hidden in form.hidden_fields %}
                        {{ hidden }}
                        {% endfor %}
                        {% endif %}
                        {{ field.errors.as_ul }}
                        {{ field }}
                    </td>
                {% endfor %}
            </tr>
    {% endfor %}
</table>
</form>

Solution

  • You need to include {{ formset.management_form }} in your template not {{ formset.management_data }}. Docs

    <form method="POST" enctype="multipart/form-data" action=".">
        {{ formset.management_form }}
        {% csrf_token %}
        ...