[Django]-Why use management form inside template?

5👍

ManagementForm form is used by the formset to manage the collection of forms contained in the formset.

It is used to keep track of how many form instances are being displayed.

for more detail information see here

0👍

If you were to render the whole {{ formset }} in the template at once, you could then inspect your source code using your browser dev tool and see some hidden fields (<input type="hidden" />) at the beginning of your form tag.

These fields are globally for control purpose and are configured through the arguments you provide to formset_factory, allowing Django to check if the form that is sent follows right the configuration you made (i.e. accept maximum 3 forms). Django absolutely needs those hidden fields to be sent along with the form data in order to know "what to do".

This is actually what Django calls Formset Management as you mentioned.

The problem occurs when you decide to be more specific regarding form template rendering: if you select what you want to render from the formset (i.e. {% for form in formset.forms %}), you then lose the other parts that made your formset be a formset. You guessed it, I am refering to the formset management.

Therefore, we need to explicitly render it in our template since it is a part that is no longer rendered, with {{ formset.management_form }}.

Leave a comment