How to create this table used formset and extra=3?
Table:
| my name first column | second column | third column | fourth column |
| formset1.field1.label | formset1.field1 | formset2.field1 | formset3.field1 |
| formset1.field2.label | formset1.field2 | formset2.field2 | formset3.field2 |
| formset1.field3.label | formset1.field3 | formset2.field3 | formset3.field3 |
| formset1.field4.label | formset1.field4 | formset2.field4 | formset3.field4 |
In view I create standard formset:
def my_view(request, id_object):
getobject = get_object_or_404(Model, id=id_object)
FormSetInit = inlineformset_factory(Model, Model2, form=FormModel2, extra=3)
FormSet = FormSetInit(instance=getobject, prefix='model2')
response = {}
response['Formset'] = FormSet
return render_to_response('my_template.html', response)
In template 'my_template.html' i try used this:
<table>
{% for form in Formset %}
{% if forloop.counter == 1 %}
{% for field in form %}
{% if forloop.counter < 8 %}
<tr>
<td class="first">{{ field.label }}</td>
<td>{{ field }}</td>
<td>{{ field }}</td>
<td class="last">{{ field }}</td>
{% if forloop.counter != 7 %}
</tr>
{% endif %}
{% else %}
{{ field }}
{% if forloop.last %} </tr> {% endif %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
</table>
But this not a good idea.. :/
I was having trouble with this too but managed to get it to work with some help from https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#displaying-a-form-using-a-template
Anyways, this is what you can try for the template, it basically iterates through each of the forms:
<table>
<thead>
{% for form in Formset.forms %}
{% if forloop.first %}
{% for field in form %}
<th>{{ field.label_tag }}</th>
{% endfor %}
{% endif %}
</thead>
<tbody>
<tr>
{% for field in form %}
<td>{{ field }}</td>
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>
Hope that helps! Not sure if it's the best way to do it, but it worked!
<table id='supertable' border=1>
<tr>
{% for form in formset %}
{% if forloop.counter = 1 %}
<td>
<table>
{% for field in form.visible_fields %}
<tr><td style="background: cyan; line-height:1; white-space:nowrap;" >{{ field.label_tag }}</td></tr>
{% endfor %}
</table>
</td>
{% endif %}
<td>
<table>
{% for field in form.visible_fields %}
<tr><td style="background: pink; line-height:1; white-space:nowrap;" >{{ field}}</td></tr>
{% endfor %}
</table>
</td>
{% endfor %}
</tr>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With