如何在 django 中使用单个提交按钮提交多个表单? [英] How do I submit multiple forms with a single submit button in django?

查看:32
本文介绍了如何在 django 中使用单个提交按钮提交多个表单?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设法使用 modelformset_factory 创建了我需要的表单.

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)

目前正在为找到的每一行生成以下 HTML:

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.

有什么想法吗?

我最终使用了 django-crispy-forms,它允许我生成输入对于每一行,然后我只是手动添加表单并提交.

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 %} 

推荐答案

阅读更多模型表单集.您不需要有单独的 form 标签,这是使用表单集的全部意义.

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>

此外,每次您在页面上多次使用 id 属性时……开发人员都会哭着睡在世界的某个地方.

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

这篇关于如何在 django 中使用单个提交按钮提交多个表单?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆