I would like to out a number of checkboxes in one of my templates and I can't get it to work.
I'm trying to pass an array containing forms.BooleanField() in my form like :
class ProjetSettingsForm(forms.Form):
## A bunch of form fields of no interest
arrayCheck = []
cb1 = forms.BooleanField()
cb2 = forms.BooleanField()
arrayCheck.append(cb1)
arrayCheck.append(cb2)
and output it like this in my template
{% for a in form.arrayCheck %}
{{ a }}
{% endfor %}
My form is called by a view :
def settings(request):
if request.method == "POST" and (request.POST.get("settings_task_type", "") == "Enregistrer Task Type"):
form = ProjetSettingsForm(request.POST, proj_id=request.session['proj'])
if form.is_valid():
settings = form.save_task_type()
c = {'proj':proj, 'form':form, 'settings':settings}
return render_to_response('projet/settings.html', c, context_instance=RequestContext(request))
else:
form = ProjetSettingsForm(proj_id=request.session['proj'])
settings = ""
c = {'proj':proj, 'form':form, 'settings':settings}
return render_to_response('projet/settings.html', c, context_instance=RequestContext(request))
But it displays :
<django.forms.fields.BooleanField object at 0xb595a2ec> <django.forms.fields.BooleanField object at 0xb595a22c>
How can I get it to display the checkboxes correctly ?
The reason I need to pass the checkboxes as an array and not one by one is that there will be many of them and I won't know in advance their exact number, it depends on a sql query.
EDIT:
If someone comes across this post, i found a solution. I don't use a form when I need to pass an array of checkboxes.
I send the array directly from my view, create the checkboxes in the template and get the results in request.POST.getlist('my_array')