forms.py
class TypeSelectionForm(forms.Form):
checkbox_field = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple(), label=\"\", required=Fals
From the documentation:
New in Django 1.4.
For more granular control over the generated markup, you can loop over the radio buttons in the template. Assuming a form
myformwith a fieldbeatlesthat uses aRadioSelectas its widget:{% for radio in myform.beatles %}{{ radio }}{% endfor %}
In your template, you should have this:
{% for radio in types.checkbox_field %}
{{ radio }}
{% endfor %}
You should also use a ModelMultipleChoiceField:
class TypeSelectionForm(forms.Form):
checkbox_field = forms.ModelMultipleChoiceField(label="",
queryset=Types.objects.none(),
required=False)
def __init__(self, *args, **kwargs):
qs = kwargs.pop('queryset')
super(TypeSelectionForm, self).__init__(*args, **kwargs)
self.fields['checkbox_field'].queryset = qs
Initiate it like this from your view:
def types(method):
""""""""""""
qs = Types.objects.filter(parent_type_id=type_id,is_active=True)
types = TypeSelectionForm(queryset=qs)
return render(request,'types.html',{'types':'types'})