How to custom checkbox and radio in Django using Bootstrap?
Asked Answered
P

1

3

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>

Expected result: custom checkbox and radio in Django using bootstrap

Portage answered 2/4, 2019 at 23:28 Comment(4)
Not all of those classes are for the checkbox. Some are for the div surrounding the checkboxImpresa
Thank you @CarlBrubaker. I updated my question based on your comment. However, the HTML was not rendered correctly.Darelldarelle
Try thisImpresa
Thank you @CarlBrubaker. I solved my problem by rendering manually, as described below.Darelldarelle
P
6

My best solution so far. I had to render manually.

template.html

<!-- radio -->
<div class="form-group">
    {{ form.field_name.label_tag }}
    {% for pk, choice in form.field_name.field.widget.choices %}
    <div class="custom-control custom-radio custom-control-inline">
        <input id="id_{{form.field_name.name}}_{{ forloop.counter0 }}" name="{{form.field_name.name}}" type="{{form.field_name.field.widget.input_type}}" value="{{pk}}" class="custom-control-input"
         {% ifequal form.field_name.data pk.0 %}
           checked="checked"
         {% endifequal %}/>
        <label for="id_{{form.field_name.name}}_{{ forloop.counter0 }}" class="custom-control-label">{{ choice }}</label>
    </div>
    {% endfor %}
</div>

<!-- checkbox -->
<div class="form-group">
    {{ form.field_name.label_tag }}
    {% for pk, choice in form.field_name.field.widget.choices %}
    <div class="custom-control custom-checkbox custom-control-inline">
        <input id="id_{{form.field_name.name}}_{{ forloop.counter0 }}" name="{{form.field_name.name}}" type="{{form.field_name.field.widget.input_type}}" value="{{pk}}" class="custom-control-input"
         {% ifequal form.field_name.data pk.0 %}
           checked="checked"
         {% endifequal %}/>
        <label for="id_{{form.field_name.name}}_{{ forloop.counter0 }}" class="custom-control-label">{{ choice }}</label>
    </div>
    {% endfor %}
</div>

My result

Portage answered 12/4, 2019 at 19:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.