I used CheckboxSelectMultiple as a widget for MultipleChoiceField. I know it makes something like this:
<ul>
<li><input type="checkbox" name="..." ></li>
...
</ul>
when I set an attribute to CheckboxSelectMultiple, django adds it to ul tag. my question is, how can I set attributes to li?
You can proceed that way
Django provides a ul list for MultipleChoiceField
. I give you an example since I do not have any clue of your codes
forms.py
OPTIONS = (
("B", "Black"),
("Y", "Yellow"),
("R", "Red"),
)
colors = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple,choices=OPTIONS)
send the form to the template through your_view
template.html
you would have called it like with {{form.colors}}
. This way will let django generates its pre-built html, whereas you can loop through it:
<ul>
{% for check in form.colors %}
<li class='your class'>{{check.tag}} <!-- this is the checkbox input generated by django -->
<label for="{{check.id_for_label}}">{{check.choice_label}}</label></li>
{% endfor %}
</ul>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.