Skip to content Skip to sidebar Skip to footer

Python - Reading Checkboxes

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

Solution 1:

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.

Solution 2:

checkbox = request.POST.getlist("common_name")

Solution 3:

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)

Post a Comment for "Python - Reading Checkboxes"