2

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?

Anuj TBE
  • 9,198
  • 27
  • 136
  • 285
  • You can have look at the django documentation [Rendering fields manually](https://docs.djangoproject.com/en/1.11/topics/forms/#rendering-fields-manually) – Astik Anand Sep 21 '17 at 07:13
  • This will work fine for `business` model as that is the parent model. How to render fields for `business_address` fields? as this is `inlineformset`. – Anuj TBE Sep 21 '17 at 07:15
  • I tried with `business_address.line_1.label` But it prints nothing – Anuj TBE Sep 21 '17 at 07:21
  • got it using `{{ business_address.form.line_1.label }}` – Anuj TBE Sep 21 '17 at 07:27

0 Answers0