I am trying to customize the checkboxes and radios in Django using bootstrap class. However, it has not worked.
I already tried to insert the bootstrap class in forms.py with widgets
and attrs
:
custom-control custom-radio custom-control-inline custom-control-input custom-control-label
My code:
models.py
Y = 1
N = 2
YN = (
(Y, 'yes'),
(N, 'no'),)
class Prop(models.Model):
yesorno = models.IntegerField(choices=YN, default=Y, verbose_name='Label...')
forms.py
class PropForm(forms.ModelForm):
class Meta:
model = Prop
exclude = ()
widgets={
'yesorno': forms.RadioSelect(
attrs={
'class':'custom-control-input custom-control-label',
}
),
}
template.html
<form action="" method="POST">
{% csrf_token %}
<div class="custom-control custom-radio custom-control-inline">
{{ form.yesorno.label_tag }} {{ form.yesorno }}
</div>
</form>
HTML rendered
<div class="custom-control custom-radio custom-control-inline">
<label for="id_yesorno_0">Label...?</label>
<ul id="id_yesorno" class="custom-control-input custom-control-label">
<li>
<label for="id_yesorno_0"><input type="radio" name="yesorno" value="1" class="custom-control-input custom-control-label" required="" id="id_yesorno_0">yes</label>
</li>
<li>
<label for="id_yesorno_1"><input type="radio" name="yesorno" value="2" class="custom-control-input custom-control-label" required="" id="id_yesorno_1" checked="">no</label>
</li>
</ul>
</div>
I would like to use the custom bootstrap style (https://getbootstrap.com/docs/4.3/components/forms/#radios).
I need to render an HTML like in the code below:
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="customRadioInline1" name="customRadioInline1" class="custom-control-input">
<label class="custom-control-label" for="customRadioInline1">Toggle this custom radio</label>
</div>
<div class="custom-control custom-radio custom-control-inline">
<input type="radio" id="customRadioInline2" name="customRadioInline1" class="custom-control-input">
<label class="custom-control-label" for="customRadioInline2">Or toggle this other custom radio</label>
</div>