简体   繁体   English

Django-如何提交多种表格

[英]Django - how to submit multiple forms

I have a page with 5 different 'tabs' (implemented using "Materialize CSS"). 我有一个包含5个“标签”的页面(使用“对CSS进行材料化”实现)。 Each tab has its own form (I have 5 different forms, I guess it's not a good idea to have one form for all the different tabsa). 每个标签都有自己的形式(我有5种不同的形式,我想对所有不同的标签都使用一种形式不是一个好主意)。

What I want to do is, when I click "submit" button, all the forms/tabs that are filled out should be submitted (and all the empty ones should not do anything). 我想做的是,当我单击“提交”按钮时,应提交所有填写的表单/选项卡(所有空白的表单/选项卡都不应做任何事情)。

Any suggestions how should I implement this? 有什么建议我应该如何实现呢?

Thanks! 谢谢!

You can use self.data in the method to access the POST data before validation. 您可以在验证之前在方法中使用self.data访问POST数据。 It should contain a key called sub1 or sub2 depending on which button was pressed. 它应包含一个名为sub1或sub2的键,具体取决于所按下的按钮。

 <form method='POST'>
{{form1.as_p}}
<button type="submit" name="sub1">Save Changes</button>
</form>
<form method='POST'>
{{form2.as_p}}
<button type="submit" name="sub2">Save Changes</button>
</form>

code

if request.method=='POST' and 'sub1' in request.POST:
    do something...
if request.method=='POST' and 'sub2' in request.POST:
    do something...

Hope for helping you . 希望对您有所帮助。

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

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