且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用Django动态将字段添加到模型表单

更新时间:2023-12-01 20:57:40

感谢维杰!我无法将您的答案标记为答案(不确定原因),但是您的链接帮助了我.

Thanks Vijay! I couldn't mark yours as the answer (not sure why) but your link helped me.

由于我只想重复一些字段,因此我只将这些字段放在表中.

Since I only wanted some fields to repeat, I put only those fields in the table.

<form id="myForm" method="post" action="">
{% for form in formset.forms %}
    <p> {{ form.nonDuplicatingField }}
    <p> {{ fomr.anotherNonDuplicatingField }}
    <table border="0" cellpadding="0" cellspacing="0">
        <tbody>
            <tr>
               <td>{{ form.duplicatingField }}</td>
               <td>{{ form.duplicatingFieldTwo }}</td>
               <td>{{ form.duplicatingFieldThree }}</td>
            </tr>
        </tbody>
    </table>
{% endfor %}
{{ formset.management_form }}
</form>

我包括了这个javascript(除了jquery和jquery.formset之外)

And I included this javascript (in addition to jquery and jquery.formset)

<script type="text/javascript">
    $(function() {
        $('#myForm tbody tr').formset();
    })
</script>