[英]How can I set attributes to li tags in CheckboxSelectMultiple?
I used CheckboxSelectMultiple as a widget for MultipleChoiceField. 我使用CheckboxSelectMultiple作为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. 当我将属性设置为CheckboxSelectMultiple时,django将其添加到ul标签。 my question is, how can I set attributes to li? 我的问题是,如何将属性设置为li?
You can proceed that way 你可以那样做
Django provides a ul list for MultipleChoiceField
. Django为MultipleChoiceField
提供ul列表。 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
通过your_view
将表单发送到模板
template.html template.html
you would have called it like with {{form.colors}}
. 您可能会像{{form.colors}}
那样调用它。 This way will let django generates its pre-built html, whereas you can loop through it: 这种方式将使django生成其预构建的html,而您可以循环遍历它:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.