如何使用 formset 和 extra=3 创建此表?
桌子:
| 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 |
在视图中,我创建了标准表单集:
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)
在模板'my_template.html'中我尝试使用这个:
<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>
但这不是一个好主意..:/