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

rab*_*ron 6 css python django twitter-bootstrap-3

forms.py

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

将其传递到模板后:

<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>
Run Code Online (Sandbox Code Playgroud)

现在我想摆脱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"> lily
  </label>
  <label class="btn btn-primary">
    <input id="id_share_1" name="share" type="checkbox" value="1"> rabbit
  </label>
</div>
Run Code Online (Sandbox Code Playgroud)

如果有人可以给我一个通用的解决方案,而不是从views.py传递特定的值,那将是理想的,我的想法是编写一个小部件,但我无法弄清楚如何.

我知道我是菜鸟,请帮忙.

yuv*_*uvi 13

从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>
Run Code Online (Sandbox Code Playgroud)