Home > Enterprise >  Is it ok to have two or more Django forms in one HTML form?
Is it ok to have two or more Django forms in one HTML form?

Time:11-29

I know it's possible to have more than one Django form in one HTML form, even formsets, and standard forms combined and it works perfectly fine. I want to know if it is a good practice and how this can impact security if it will at all? I couldn't find any information about those cases but in tutorials, I saw both (multiple Django forms in one HTML and every Django form in a separate HTML form) in some cases is necessary since the two forms can do different things like update and delete for example. In my case, all forms POST data to different models, and then all are combined in one model. Please take a look at the example (This is a simple example and not the actual code) below:

<form action="" method="POST">

{% csrf_token %}

{{ form.field1 }}
{{ form.field2 }}
{{ form.field3 }}


{% for field in dates_form %}
    {{ field.Loading_date_from }}{{ field.Loading_time_from }}{{ field.Loading_date_to }}{{ field.Loading_time_to }}
{% endfor %}

{% for field in dates_form2 %}
    {{ field.Loading_date_from }}{{ field.Loading_time_from }}{{ field.Loading_date_to }}{{ field.Loading_time_to }}
{% endfor %}

{{ form.field4 }}
{{ form.field5 }}


<input type="submit">

</form>

CodePudding user response:

Yes, it is okay, as long as you handle them properly in the view. There’s a package to help with this, including support for model forms as well:

https://github.com/kennethlove/django-shapeshifter

Full disclosure, I’m a contributor to the package.

  • Related