简体   繁体   English

django :: 如何在表单中设置 CheckboxSelectMultiple 的样式?

[英]django :: How to style a CheckboxSelectMultiple in a form?

forms.py表格.py

class FormEntry(forms.ModelForm):
  class Meta:
    model = Entry
    fields = ['name','price','share']
    widgets = {
      'share':forms.CheckboxSelectMultiple(),
    }

after passing it into a template:将其传递到模板后:

<ul id="id_share">
<li><label for="id_share_0"><input id="id_share_0" name="share" type="checkbox" value="2"> lily</label></li>
<li><label for="id_share_1"><input id="id_share_1" name="share" type="checkbox" value="1"> rabbit</label></li>
</ul>

now i want to get rid of the ul and li, also, I would like to use bootstrap3's button group to style it, something like this现在我想摆脱 ul 和 li,另外,我想使用 bootstrap3 的按钮组来设置它的样式,就像这样

<div class="btn-group" data-toggle="buttons">
  <label class="btn btn-primary">
    <input id="id_share_0" name="share" type="checkbox" value="2"> aaa
  </label>
  <label class="btn btn-primary">
    <input id="id_share_1" name="share" type="checkbox" value="1"> bbb
  </label>
</div>

It would be ideal if someone can give me a general solution, instead of passing specific values from views.py, my idea is to write a widget, but I just can't figure out how.如果有人能给我一个通用的解决方案,而不是从 views.py 传递特定值,那将是理想的,我的想法是编写一个小部件,但我无法弄清楚如何。

As of Django 1.6, you can loop :从 Django 1.6 开始, 您可以循环

<div class="btn-group" data-toggle="buttons"> 
{% for checkbox in myform.shares %}
    <label class="btn btn-primary">
    {{ checkbox.tag }} {{ checkbox.choice_label }}
    </label>
{% endfor %}
</div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM