I have a question, what happens is that I have a formset and an empty_form (in the same HTML); I have them to do some calculations, for the empty_form I already managed to extract the ID and do operations but not for the formset, and that is that my main problem is that they have the different ID, for example for the formset is like this:
id_form-0-quantity
and for the empty_form it is:
id__form-1-quantity (one more underscore)
but with that different ID I have to make some changes in JS, which I don’t want to do because I’m very new in JS and possibly messing up the code more. Is there a way to change the formset prefix to look like this: id__form-0-quantity?;
I used the following line:
formset = ParteFormSet(request.POST, request.FILES, prefix='__form')
But absolutely nothing happens
views
def create_Presupuestos(request):
extra_forms = 1
ParteFormSet = formset_factory(PresupuestosParteForm, extra=extra_forms, max_num=20)
presupuestosclientesform=PresupuestosClientesForm(request.POST or None)
presupuestosvehiculosform=PresupuestosVehiculosForm(request.POST or None)
presupuestosparteform=PresupuestosParteForm(request.POST or None)
presupuestosmanoobraform=PresupuestosManoObraForm(request.POST or None)
presupuestospagosform=PresupuestosPagosForm(request.POST or None)
if request.method == 'POST':
formset = ParteFormSet(request.POST, request.FILES, prefix='__form')
if formset.is_valid():
presupuestosclientesform.save()
return redirect('presupuestos:index')
else:
formset = ParteFormSet()
return render(request,'Presupuestos/new-customer.html',{'presupuestosclientesform':presupuestosclientesform,'presupuestosvehiculosform':presupuestosvehiculosform,'presupuestosparteform':presupuestosparteform,'presupuestosmanoobraform':presupuestosmanoobraform,'presupuestospagosform':presupuestospagosform,'formset':formset})
HTML
<table class="table table-bordered table-nowrap align-middle" id="childTable1">
<thead class="table-info">
<tr>
<th scope="col">Quantity</th>
<th scope="col">Unit Price</th>
</thead>
<tbody>
{{ formset.management_form }}
{% for form in formset %}
<div class="part-form">
<tr>
<td>
{{form.quantity}}
<!-- <input type="text" name="parts_quantity" class="form-control input-new-customer-quantity" />-->
</td>
<td>
{{form.unit_price}}
<!-- <input type="text" name="parts_unit_price" class="form-control input-new-customer-unit-price" />-->
</td>
</tr>
</div>
{% endfor %}
</tbody>
</table>
<div class="part-form table-responsive" id="empty-row">
<table class="table table-bordered table-nowrap align-middle">
<tr>
<td>{{formset.empty_form.quantity}}</td>
<td>{{formset.empty_form.unit_price}}</td>
</tr>
</table>
</div>
>Solution :
You should also include the prefix in the case of a GET request, otherwise it will not prefix that form when you load the page for the first time:
def create_Presupuestos(request):
extra_forms = 1
ParteFormSet = formset_factory(PresupuestosParteForm, extra=extra_forms, max_num=20)
presupuestosclientesform=PresupuestosClientesForm(request.POST or None)
presupuestosvehiculosform=PresupuestosVehiculosForm(request.POST or None)
presupuestosparteform=PresupuestosParteForm(request.POST or None)
presupuestosmanoobraform=PresupuestosManoObraForm(request.POST or None)
presupuestospagosform=PresupuestosPagosForm(request.POST or None)
if request.method == 'POST':
formset = ParteFormSet(request.POST, request.FILES, prefix='__form')
if formset.is_valid():
presupuestosclientesform.save()
return redirect('presupuestos:index')
else:
# prefix in case of a GET 🖟
formset = ParteFormSet(prefix='__form')
return render(request,'Presupuestos/new-customer.html',{'presupuestosclientesform':presupuestosclientesform,'presupuestosvehiculosform':presupuestosvehiculosform,'presupuestosparteform':presupuestosparteform,'presupuestosmanoobraform':presupuestosmanoobraform,'presupuestospagosform':presupuestospagosform,'formset':formset})