Django formset - 在 table 中呈现多余的行
Django formset - surplus line is rendered in the table
我使用 Django 表单集成功渲染了一个 table,但是最后渲染了一个多余的空行!我做错了什么?
<table id="formset" class="form table table-condensed zeon zeon-row-hover">
{{ formset.management_form }}
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td class = 'zeon_input_table_cell'>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{field}}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
查看:
MyModelFormSet = modelformset_factory(MyModel, exclude=('agreement_vendors',))
formset = MyModelFormSet(queryset = MyModel.objects.filter(...))
return render_to_response('vendors/edit_agreement.html',
{'formset':formset},
context_instance=RequestContext(request))
更新:
根据下面的答案,问题解决了 extra = 0
根据 Django docs:
As you can see it only displayed one empty form. The number of empty forms that is displayed is controlled by the extra parameter. By default, formset_factory() defines one extra form; the following example will display two blank forms:
考虑到这一点,Django 文档建议执行类似的操作来设置额外表单的数量:
ArticleFormSet = formset_factory(ArticleForm, extra=2)
我使用 Django 表单集成功渲染了一个 table,但是最后渲染了一个多余的空行!我做错了什么?
<table id="formset" class="form table table-condensed zeon zeon-row-hover">
{{ formset.management_form }}
{% for form in formset.forms %}
{% if forloop.first %}
<thead><tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}
{% endfor %}
</tr></thead>
{% endif %}
<tr class="{% cycle row1,row2 %}">
{% for field in form.visible_fields %}
<td class = 'zeon_input_table_cell'>
{# Include the hidden fields in the form #}
{% if forloop.first %}
{% for hidden in form.hidden_fields %}
{{ hidden }}
{% endfor %}
{% endif %}
{{ field.errors.as_ul }}
{{field}}
</td>
{% endfor %}
</tr>
{% endfor %}
</table>
查看:
MyModelFormSet = modelformset_factory(MyModel, exclude=('agreement_vendors',))
formset = MyModelFormSet(queryset = MyModel.objects.filter(...))
return render_to_response('vendors/edit_agreement.html',
{'formset':formset},
context_instance=RequestContext(request))
更新:
根据下面的答案,问题解决了 extra = 0
根据 Django docs:
As you can see it only displayed one empty form. The number of empty forms that is displayed is controlled by the extra parameter. By default, formset_factory() defines one extra form; the following example will display two blank forms:
考虑到这一点,Django 文档建议执行类似的操作来设置额外表单的数量:
ArticleFormSet = formset_factory(ArticleForm, extra=2)