如何在Django中只在ModelForms表中正确提交一个表单?

问题描述 投票:-1回答:1

我需要创建一个表格表,用于根据我的自定义用户模型创建和更新用户。它必须看起来像这样:enter image description here表格在表格中显示为特定行。 Save按钮保存提交表单,Delete从数据库中删除表单和模型实例,Add按钮将新的空白表单附加到<tbody>的结尾但是为了保存新表单我们需要单击Save按钮。

现在我卡住了,不知道如何正确实现上面的逻辑。我需要每个表单都要独立更新和提交。我查找了基于ModelFormSet的解决方案,但它提交了所有表单,并且不允许仅更新一个特定的数据行。

我们如何创建预先填充的ModelFormSet,无论表中的其他形式如何,都可以提交每行表单数据?像这样:

<form action="" method="post">
<tr>
<td>1</td>
<td> <input type="text" name="username" value="admin" maxlength="255" class="login-input" id="id_username" /></td>
<td> <input type="text" name="password" value="" maxlength="128" class="password-input" id="id_password" /></td>
<td> <input type="text" name="email" maxlength="255" class="email-input" id="id_email" /></td>
<td> <input type="checkbox" name="active" class="checkbox-input" id="id_active" checked /></td>
<td> <input type="checkbox" name="staff" class="checkbox-input" id="id_staff" checked /></td>
<td> <input type="checkbox" name="reboot_field" class="checkbox-input" id="id_reboot_field" checked /></td>
<td><button type="submit" form="form1">Save</button></td>
<td><button form="form1" >Delete</button></td>
<tr>
</form> 
<form action="" method="post">
<tr>
<td>2</td>
<td> <input type="text" name="username" value="user1" maxlength="255" class="login-input" id="id_username" /></td>
<td> <input type="text" name="password" value="" maxlength="128" class="password-input" id="id_password" /></td>
<td> <input type="text" name="email" maxlength="255" class="email-input" id="id_email" /></td>
<td> <input type="checkbox" name="active" class="checkbox-input" id="id_active" checked /></td>
<td> <input type="checkbox" name="staff" class="checkbox-input" id="id_staff" checked /></td>
<td> <input type="checkbox" name="reboot_field" class="checkbox-input" id="id_reboot_field" checked /></td>
<td><button type="submit" form="form2" >Save</button></td>
<td><button form="form2">Delete</button></td>
<tr>
</form> 
django django-forms django-views
1个回答
0
投票

找到了基于this链接的解决方案。

© www.soinside.com 2019 - 2024. All rights reserved.