简体   繁体   English

如何在Django中使用一个提交按钮提交多个表单?

[英]How do I submit multiple forms with a single submit button in django?

I have managed to create the forms I need using modelformset_factory. 我设法使用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: 当前,这为找到的每一行生成以下HTML:

<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. 我最终使用了django-crispy-forms ,它使我可以为每一行整理输入内容,然后我只是手动添加了表单并提交。

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

Read more into model formsets . 阅读更多关于模型表单集的信息 You don't need to have separate form tags, it's the whole point of using a formset. 您不需要单独的form标签,这是使用form的全部重点。

<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. 此外,每次在页面上多次使用id属性时,开发人员都会哭泣自己睡在世界的某个地方。

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. 我怀疑您将需要使用Ajax进行操作-否则,一旦提交一种表单,您将无法采用其他方式。

There are a few jQuery form libraries that should make it relatively straightforward. 有一些jQuery表单库应该使它相对简单。 For example, http://malsup.com/jquery/form/ . 例如, 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. 当然,您将需要处理错误并等待所有表单提交。

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. 如果我没记错的话,那么您将尝试为单个avaluo对象编辑许多facturas 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. 检出内联表单集工厂,而不是modelformset工厂。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM