表格.py
class TypeSelectionForm(forms.Form):
checkbox_field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), label="", required=False)
def __init__(self, type_id, *args, **kwargs):
super(TypeSelectionForm, self).__init__(*args, **kwargs)
_type_checkbox = self.fields['checkbox_field']
MY_CHOICES=((type.id, type.title) for type in type)
_type_checkbox.choices = MY_CHOICES
initial_val = []
type_selection = Types.objects.filter(parent_type_id=type_id,is_active=True)
for type_selection in type_selection:
initial_val.append(type_selection.id)
_type_checkbox.initial = initial_val
视图.py
def types(method):
""""""""""""
types = TypeSelectionForm(type_id)
return render(request,'types.html',{'types':types})
在模板中,我正在渲染这样的字段,
类型.html
{% for field in types.checkbox_field %}
<div class="deletelist">
{{field}}<br />
</div>
{% endfor %}
它正在生成这样的html,
<ul>
<li><label for="id_checkbox_field_0"><input checked="checked" type="checkbox" name="checkbox_field" value="597" id="id_checkbox_field_0" /> comp lab</label></li>
<li><label for="id_checkbox_field_1"><input checked="checked" type="checkbox" name="checkbox_field" value="598" id="id_checkbox_field_1" /> phy lab</label></li>
<li><label for="id_checkbox_field_2"><input checked="checked" type="checkbox" name="checkbox_field" value="599" id="id_checkbox_field_2" /> chem lab</label></li>
</ul>
我想<ul>
用<li>
<div class="class-name">
需要帮忙。