Django styling multichoicefield checkboxes

时间:2018-03-25 20:02:44

标签: django

I was able to include multi select checkboxes however, I am having trouble to provide styling to the forms. I can can correctly displaying the options but they appear with a dot in front. When I add a class to the template adding attributes to the widget it adds the class but its not allowing style changes. I try to do it with django widget tweeks and it does not do it either. I think to style the label of the checkbox the class should be inserted to the checkbox label and not to the input.

forms.py

 CHOICES = (
    ('a', 'A'),
    ('b', 'b'),
    ('c', 'c'),

)

 class Modalities(forms.Form):
    multicheck = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(
        attrs={'class': 'check_label'}
    ),
    choices=CHOICES)

HTML

<div class="col-md-6">
                    <div class="form-group">
                    <h3>Options</h3>
                    </div>


                    {% for field in form %}
                    {{ field }}
                    {% endfor %}

                    </div>

CSS

label {
    font: bold 15px "Helvetica Neue";
    text-transform: lowercase;

}

.check_label {
  font: 12px  "Helvetica Neue" !important;
  text-transform: initial !important;


}

Output: enter image description here

enter image description here

1 个答案:

答案 0 :(得分:0)

有问题的点是因为您的复选框位于<li>元素内,该点不是复选框输入本身的一部分。

如果您希望将其删除,可以使用类包装整个表单,然后添加:

.bulletless-form ul li { list-style: none; }

或手动为每个字段ID执行相同操作。

或者,为复选框编写自己的小部件,这将使其无需列表。