forms.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
<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.
As of Django 1.6, you can 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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With