我在用着Django 1.11
我有一个CreateView
withinlineformset
来存储关联的模型记录。
forms.py
class BusinessForm(ModelForm):
class Meta:
model = Business
exclude = ()
BusinessAddressFormSet = inlineformset_factory(
Business,
BusinessAddress,
form=BusinessForm,
extra=1,
can_delete=False
)
而在 中business_form.html
,我正在做的是
<form method="post">
{% csrf_token %}
// render business form
{{ form.as_p }}
// render business address fields
{{ business_address.management_form }}
<table class="table">
{{ business_address.management_form }}
{% for form in business_address.forms %}
{% if forloop.first %}
<thead>
<tr>
{% for field in form.visible_fields %}
<th>{{ field.label|capfirst }}</th>
{% endfor %}
</tr>
</thead>
{% endif %}
<tr class="{% cycle 'row1' 'row2' %} formset_row">
{% for field in form.visible_fields %}
<td>
{# 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>
这将地址模型的所有字段呈现为
- line_1
- line_2
- city
- state
- postal
上述字段以table row
. 因为,我有一个单独的 html 模板与不同字段的内联表单图标一起使用。
如何手动渲染字段business_address
以及错误字段和隐藏字段?