0

我正在使用django-dynamic-formset将表单动态添加到表单集中。
只要我extra=1在创建表单集时可以正常工作。
不过,我想做的是在用户单击“添加表单”按钮之前不显示任何表单。推荐
的解决方案是使用,但由于某种原因不起作用(不起作用意味着没有记录保存到数据库中)。按照工作代码,不使用empty_form: empty_forms

<div id="coding">
<div id="coding-inner">
    <form action="" method="post" accept-charset="utf-8" id="form"> 
        {% csrf_token %}
        {{ formset.management_form }}
        {% for form in formset %}
        <div class="form-container">
            <table border="0" cellspacing="5" cellpadding="5">
                <tr>
                    <td><label for="id_form-0-event_date">Event Date:</label></td>
                    <td>{{ form.event_date}}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-location">Location:</label></td>
                    <td><div class="location_wrapper">{{ form.location }}</div></td>
                </tr>
                <tr>
                    <td><label for="id_form-0-actors">Actors:</label></td>
                    <td>{{ form.actors }}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-num_participants">Number of Participants:</label></td>
                    <td>{{ form.num_participants }}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-issue">Issue:</label></td>
                    <td>{{ form.issue }}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-side">Side:</label></td>
                    <td>{{ form.side }}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-scope">Scope:</label></td>
                    <td>{{ form.scope }}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-part_violence">Participant Violence:</label></td>
                    <td>{{ form.part_violence}}</td>
                </tr>
                <tr>
                    <td><label for="id_form-0-sec_engagement">Security Forces Engagment:</label></td>
                    <td>{{ form.sec_engagement }}</td>
                </tr>
            </table>
        </div>
        {% endfor %}
        <div id="form-nav">
            <div id="save-stay">
                <input type="submit" name="coding_form_save" value="Save">
            </div>
            <div id="save-next">
                <input type="submit" name="coding_form_next" value="Save &#38; Next">
            </div>

        </div>
    </form>

</div>

<div id="pagination">
    <span class="step-links">
        {% if articles.has_previous %}
        <a href="?page={{ articles.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Article {{ articles.number }} of {{ articles.paginator.num_pages }}.
        </span>
        {% if articles.has_next %}
        <a href="?page={{ articles.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>


</div>

</div>

<script type="text/javascript">


           $(function() {
               $('.form-container').formset({
                   prefix: '{{ formset.prefix }}',
                   addText: 'add Event',
                   deleteText: 'delete Event'
               });
           })
</script>

现在使用的代码empty_form不起作用。唯一的其他区别是,对于使用 的代码empty_formextra将其设置为 0 而不是 1。

<div id="coding">
<div id="coding-inner">
    {% with formset.empty_form as form %}
    <div id="form-container" style="display:none;">
        <table border="0" cellspacing="5" cellpadding="5">
            <tr>
                <td><label for="id_form-0-event_date">Event Date:</label></td>
                <td>{{ form.event_date}}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-location">Location:</label></td>
                <td><div class="location_wrapper">{{ form.location }}</div></td>
            </tr>
            <tr>
                <td><label for="id_form-0-actors">Actors:</label></td>
                <td>{{ form.actors }}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-num_participants">Number of Participants:</label></td>
                <td>{{ form.num_participants }}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-issue">Issue:</label></td>
                <td>{{ form.issue }}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-side">Side:</label></td>
                <td>{{ form.side }}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-scope">Scope:</label></td>
                <td>{{ form.scope }}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-part_violence">Participant Violence:</label></td>
                <td>{{ form.part_violence}}</td>
            </tr>
            <tr>
                <td><label for="id_form-0-sec_engagement">Security Forces Engagment:</label></td>
                <td>{{ form.sec_engagement }}</td>
            </tr>
        </table>
    </div>
    {% endwith %}
    <form action="" method="post" accept-charset="utf-8" id="form"> 
        {% csrf_token %}

        <div id="forms">

        </div>
        {{ formset.management_form }}
        <div id="form-nav">
            <div id="save-stay">
                <input type="submit" name="coding_form_save" value="Save">
            </div>
            <div id="save-next">
                <input type="submit" name="coding_form_next" value="Save &#38; Next">
            </div>
        </div>

    </form>

</div>

<div id="pagination">
    <span class="step-links">
        {% if articles.has_previous %}
        <a href="?page={{ articles.previous_page_number }}">previous</a>
        {% endif %}

        <span class="current">
            Article {{ articles.number }} of {{ articles.paginator.num_pages }}.
        </span>
        {% if articles.has_next %}
        <a href="?page={{ articles.next_page_number }}">next</a>
        {% endif %}
    </span>
</div>
</div>

<script type="text/javascript">
           $(function() {
               $('#forms').formset({
                   formTemplate: '#form-container',
                   prefix: '{{ formset.prefix }}',
                   addText: 'add Event',
                   deleteText: 'delete Event'
               });
           })

</script>

我错过了什么?

4

1 回答 1

-1

你可以使用这个jquery 插件

于 2013-07-25T08:41:07.147 回答