2

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')

1 Answer 1

3

Don't use form fields outside a form. They don't work like that. Create a form with a MultipleChoiceField.

Sign up to request clarification or add additional context in comments.

6 Comments

What do you mean by that ? I created the fields inside a form
No you didn't. They're in a list. Use a Django form class.
They are in a class class ProjetSettingsForm(forms.Form): I just didn't put it in my question
Then what's arrayCheck? This makes no sense without seeing the full code.
It's a variable of the Form class. I know I'm making something wrong, but I don't know what
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.