Skip to content Skip to sidebar Skip to footer

Django Modelformset_factory Doesn't Include Actual Forms

I've been trying to follow tutorials and other SO questions and have a modelformset_factory that's displaying a list of what looks like forms in the html, but it turns out they're

Solution 1:

As described in the formset documention you must add the form tag manually. This is not very different from what you do when displaying a single form.

It appears that you are iterating through the formset and displayig them one by one. That means you must also add the management form

<formmethod="post"action="">
    {{ formset.management_form }}
    <div ='container'>
        {% for form in formset %}
            <divclass='row'>{{form}} <inputtype="submit"value="Update" /></div>
        {% endfor %}
    <div></form>

Or you will get errors about a missing or misconfigured management form.

Solution 2:

Note that it does not include the tags, or a submit button. We’ll have to provide those ourselves in the template.

Read more: Working with Forms: Building a form in Django

The reason you are not getting the <form> tag is because from a logical point of view a form validation can be handled anywhere in your application. That's why you need to specify the form tag explicitly with the target url (good to use reverse(view_name)), method and other parameters.

Post a Comment for "Django Modelformset_factory Doesn't Include Actual Forms"