I'm trying to create a form with radio button choices with Django. The website is designed in bootstrap 4 and for some reason, the buttons dissapear when the custom-control-input class is applied.
I have started with a static template that is rendering perfectly. The code for these button groups is the following:
<div class="form-group row">
<div class="col-sm-2">Sexo</div>
<div class="col-sm-10">
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" id="sexo1" name="sexoRadios">
<label class="custom-control-label" for="sexo1">Mujer</label>
</div>
<div class="custom-control custom-radio">
<input type="radio" class="custom-control-input" id="sexo2" name="sexoRadios">
<label class="custom-control-label" for="sexo2">Hombre</label>
</div>
</div>
</div>
To create the form in Django, I have added this lines to the forms.py file:
sexo_choices=[('hombre','Hombre'),
('mujer','Mujer')]
sexo = forms.ChoiceField(choices=sexo_choices, widget=forms.RadioSelect(
attrs={'class':'custom-control-input'}
))
This makes the radio buttons to dissapear for some reason. If I analyze the HTML created by django, it seems quite similar to the static web.
<div class="form-group row">
<div class="col-sm-2">Sexo</div>
<div class="col-sm-10">
<div class="custom-control custom-radio">
<label for="id_sexo_0"><input type="radio" name="sexo" value="hombre" class="custom-control-input" id="id_sexo_0" required>Hombre</label>
</div>
<div class="custom-control custom-radio">
<label for="id_sexo_1"><input type="radio" name="sexo" value="mujer" class="custom-control-input" id="id_sexo_1" required>Mujer</label>
</div>
</div>
</div>
However, when I inspect the object with Google Chrome, I observe that the input tag has the following CSS attributes:
.custom-control-input {
position: absolute;
z-index: -1;
opacity: 0;
}
It seems the problem comes from the class of the label tag. Is there a way to assign a class to the label?
Thank you very much.