views:

317

answers:

3

Hello.

I have a few checkboxes with common name and individual variables (ID). How can I in python read them as list? Now I'm using

checkbox= request.POST["common_name"]

It isn't work properly, checkbox variable store only the last checked box instead of any list or something.

+6  A: 

If you were using WebOB, request.POST.getall('common_name') would give you a list of all the POST variables with the name 'common_name'. See the WebOB docs for more.

But you aren't - you're using Django. See the QueryDict docs for several ways to do this - request.POST.getlist('common_name') is one way to do it.

James Polley
+2  A: 
checkbox = request.POST.getlist("common_name")
slav0nic
A: 

And if you want to select objects (say Contact objects) based upon the getlist list, you can do this:

selected_ids = request.POST.getlist('_selected_for_action')
object_list = Contact.objects.filter(pk__in=selected_ids)
Tom Tom