如何使用Bootstrap在Django中自定义复选框和广播? [英] How to custom checkbox and radio in Django using Bootstrap?

查看:99
本文介绍了如何使用Bootstrap在Django中自定义复选框和广播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用bootstrap类自定义Django中的复选框和单选按钮.但是,它没有用.

I am trying to customize the checkboxes and radios in Django using bootstrap class. However, it has not worked.

我已经尝试使用widgetsattrs将bootstrap类插入到forms.py中: custom-control custom-radio custom-control-inline custom-control-input custom-control-label

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

我的代码:

models.py

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

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呈现

<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>

我想使用自定义引导程序样式( https://getbootstrap. com/docs/4.3/components/forms/#radios ).

I would like to use the custom bootstrap style (https://getbootstrap.com/docs/4.3/components/forms/#radios).

我需要呈现如下代码中的HTML:

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>

预期结果:

推荐答案

到目前为止,我最好的解决方案. 我不得不手动渲染.

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>

这篇关于如何使用Bootstrap在Django中自定义复选框和广播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆