I'm using Django 1.11
I have a CreateView
with inlineformset
to store associated model record.
forms.py
class BusinessForm(ModelForm):
class Meta:
model = Business
exclude = ()
BusinessAddressFormSet = inlineformset_factory(
Business,
BusinessAddress,
form=BusinessForm,
extra=1,
can_delete=False
)
and in the business_form.html
, I'm doing is
<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>
This renders all field of address model as
- line_1
- line_2
- city
- state
- postal
The above fields are rendered in table row
. Since, I have a separate html template to use with inline form icon for different fields.
How can I manually render fields from business_address
along with error fields and hidden fields?