Размещение меток полей в таблице django в первой строке
Я хочу:
- print the label in the first row
- set with of each field to 45px
- put the first column where C will enumerate 1,2,3,... according to the rows.
должно быть что-то вроде этого:
V | x1 | x2 | прямая | RHS |
---|---|---|---|---|
C1 | поле | поле | поле | поле |
C2 | поле | поле | поле | поле |
C3 | поле | поле | поле | field |
в теле файла page.html:
<table>
{% for form in formset %}
<tr> {{ form.label }}
{% for item in form %}
<td style="width:10px; text-align: center">{{ item }}</td>
{% if not item.label_tag = 'direction' %}
...do something...
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</table>
в файле forms.py:
class linprog_vars(forms.Form):
x1 = forms.FloatField(label='x1')
x2 = forms.FloatField(label='x2')
direct = forms.CharField(label='direct')
rhs = forms.FloatField(label='rhs')
En views.py
def linprog(request):
extra_lines = 3
formset = formset_factory(linprog_vars, extra=extra_lines)
context = {'formset': formset}
return render(request, 'linprog/linprog.html', context)
Думаю, что-то вроде этого (не проверено). Набор форм - это список похожих форм, поэтому метки для всех них обычно одинаковые. Поэтому просто перебирайте поля в первой форме formset.0
, выбирая метки полей.
<table>
<thead>
<tr>
{% for field in formset.0 %}
<th style="..."> {{ field.label }} </th>
{% endfor %}
</tr>
</thead>
...etc as in question