我正在尝试在django formset中手动渲染字段。
<select class="form-control" id="{{ line_form.label.id_for_label }}" name="{{ line_form.label.name }}">
{% if form.label.value %}
<option value="{{ form.label.value }}">{{ form.label.value }}</option>
{% else %}
<option value="" selected>-</option>
{% endif %}
{% for item in view.items %}
<option value="{{item.id}}">{{item.name}}</option>
{% endfor %}
</select>
上述字段的名称是标签,但我想要 lines-0-label 。当我在formset中添加一个新表单时,上面的字段具有相同的名称,它应该是 lines-1-label ,并且应该增加。即, line-2-label , lines-3-label , lines-4-label ,....,行-101标签
views.py
class InvoiceCreateView(generic.CreateView):
template_name = "books/invoice_create_or_update.html"
model = Invoice
form_class = InvoiceForm
formset_class = InvoiceLineFormSet
success_url = reverse_lazy("books:invoice-list")