Note that there are some explanatory texts on larger screens.

plurals
  1. PODjango - array of checkboxes
    primarykey
    data
    text
    <p>I would like to out a number of checkboxes in one of my templates and I can't get it to work.</p> <p>I'm trying to pass an array containing forms.BooleanField() in my form like :</p> <pre><code>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) </code></pre> <p>and output it like this in my template</p> <pre><code>{% for a in form.arrayCheck %} {{ a }} {% endfor %} </code></pre> <p>My form is called by a view :</p> <pre><code>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)) </code></pre> <p>But it displays : </p> <pre><code>&lt;django.forms.fields.BooleanField object at 0xb595a2ec&gt; &lt;django.forms.fields.BooleanField object at 0xb595a22c&gt; </code></pre> <p>How can I get it to display the checkboxes correctly ?</p> <p>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.</p> <p><strong>EDIT:</strong></p> <p>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.</p> <p>I send the array directly from my view, create the checkboxes in the template and get the results in <code>request.POST.getlist('my_array')</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload