Django :: How is the style of the CheckboxSelectMultiple in the form?

forms.py

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

after passing it to the 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 ul and li, also, I would like to use the bootstrap3 button group to style it, something like this

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

It would be ideal if someone could give me a general solution, instead of passing certain values ​​from views.py, my idea is to write a widget, but I just can't figure out how to do this.

I know that I am noob, please help.

+4
source share
1 answer

As of Django 1.6, you can do a loop :

<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>
+8
source

Source: https://habr.com/ru/post/1533062/


All Articles