1

I have managed to create the forms I need using modelformset_factory.

avaluos = Avaluo.objects.filter(Estatus__contains='CONCLUIDO',Factura__isnull=True)
FacturaFormset = modelformset_factory(Avaluo,form=FacturaForm,extra=0)

Currently this is generating the following HTML for each of the rows found:

<form id="id-FacturaForm" class="blueForms" method="post">[..]</form>
<form id="id-FacturaForm" class="blueForms" method="post">[..]</form>
<form id="id-FacturaForm" class="blueForms" method="post">[..]</form>

I want to submit all the forms using a single submit button.

Any ideas?

UPDATE

I ended up using django-crispy-forms which allowed me to gerate inputs for each row, and then I just manually added the form and submit.

   self.helper.form_tag = False


{{example_formset.management_form }}
       {% for a,b in olist %}
{{ b.id }}
<tr>
    <td style="width:10px;"> {% crispy b %} </td>
    <td> {{a.id}} </td>     
</tr>
{% endfor %} 
Community
  • 1
  • 1
Gustavo Reyes
  • 1,313
  • 1
  • 17
  • 31
  • http://stackoverflow.com/questions/2374224/django-working-with-multiple-forms Could this help? – GordonsBeard Feb 27 '13 at 23:28
  • 1
    Multiple elements with the same id is invalid html an is sure to cause you trouble. – Musa Feb 27 '13 at 23:29
  • Note: in your case the "multiple forms" are in the same formset. If they're completely different forms (created by different form classes) see [python - django submit two different forms with one submit button - Stack Overflow](https://stackoverflow.com/questions/18489393/django-submit-two-different-forms-with-one-submit-button) – user202729 Aug 13 '21 at 11:27

3 Answers3

10

Read more into model formsets. You don't need to have separate form tags, it's the whole point of using a formset.

<form method="post" action="">
    {{ factura_formset.management_form }}
    <table>
        {% for form in factura_formset %}
            {{ form }}
        {% endfor %}
    </table>
</form>

Also, every time you use the id attribute more than once on a page… a developer cries themselves to sleep somewhere in the world.

Matt
  • 8,758
  • 4
  • 35
  • 64
0

I suspect you will need to do it using Ajax - otherwise as soon as one form is submitted you will not be able to go the other way.

There are a few jQuery form libraries that should make it relatively straightforward. For example, http://malsup.com/jquery/form/.

It would look something like:

$('#button-id').click(function() {
  $('.blueForms').ajaxSubmit();
});

Of course, you'll then need to deal with error handling and waiting for all the forms to have submitted.

Dan Goldin
  • 957
  • 13
  • 32
0

If you're trying to create many instances of the "same" form (this is, they all look equal), as if it were one of many childs belonging to a single, master element, you don't actually need to create a form tag for each of the formsets.

If I'm not mistaken, you're trying to edit many facturas for a single avaluo object. Am I right? The representation would be a single "avaluo" form with many inline formsets, one for each "factura".

Check out the inline formsets factory instead of the modelformset factory.

Mariano
  • 1,357
  • 3
  • 17
  • 30
  • I'm sorry, I'm reading your query again and I think I might have the relationships backwards... – Mariano Feb 27 '13 at 23:45
  • Yes exactly , I was trying to edit the _factura_ field on every _avaluo_ matching the filter. I ended up using **modelformset_factory** with **django-cripy-forms** and instead of generating forms I created inputs for each row and it worked like a charm. Thanks! – Gustavo Reyes Mar 01 '13 at 01:18