I do not know how to capture the value of a select from a form created from a ModelForm. in the template:
<div class="form-group">
<label for="{{formulario.cod_sucursal.id_for_label}}">
{{formulario.cod_sucursal.label}}</label>
<select name="{{formulario.cod_sucursal.id_for_label}}" id="{{formulario.cod_sucursal.id_for_label}}" class="custom-select form-control">
<option value='0'>Selección de Sucursal</option>
{% for opciones in formulario.cod_sucursal %}
{{opciones}}
{% endfor %}
</select>
</div>
The form is cut off because it is longer.
What is rendered is:
<form action="/tablaventas/recibo" method="post">
<input type='hidden' name='csrfmiddlewaretoken' value='0w3Y3WLmT6rbN488ohtG36LUMcS2t6XrhDu3X26V7fecg35kMkOzyv5kNXTRFu1Z' />
<div class="form-group">
<label for="encabezado-cod_sucursal">Cod sucursal</label>
<select name="encabezado-cod_sucursal" id="id_encabezado-cod_sucursal" class="custom-select form-control">
<option value='0'>Selección de Sucursal</option>
<option value="600">Pilgrim</option>
<option value="200">Mandiola</option>
<option value="100">Fch</option>
</select>
</div>
the views.py for the function is:
def tablaventas_recibo(request):
if request.method=='POST':
formu=VentasFormSet(request.POST, prefix='detalle') # formset
encabezado=tablaVCPFormulario(request.POST, prefix='encabezado') # Formulario
print(encabezado.errors)
if formu.is_valid() and encabezado.is_valid():
codigo_sucursal=encabezado.cleaned_data
co = formu.cleaned_data[1]
print(codigo_sucursal)
return HttpResponse('gracias')
return HttpResponse(encabezado.errors)
and the ModelForm is:
class tablaVCPFormulario(ModelForm):
class Meta:
model=VentasContrapuntoPilgrim
fields='__all__'
widgets = {
'titulo': TextInput,
'edicion': TextInput,
'fecha_contable':SelectDateWidget(attrs={'class': 'custom-select'}),
'cod_sucursal':SelectMultiple(choices=codigos_sucursal),
}
exclude = ('saldo','inicial','isbn',)
prefix='encabezado'
the error output tells me I need to enter cod_sucursal , just that it is in the html select in the template, but I have no idea what I will need to be captured and sent in the post . Muchach @ s, of course, very grateful.