2

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.

3
  • What framework are you using? Commented Dec 30, 2009 at 12:37
  • Based on your other question, I'm going to assume this is in Django Commented Dec 30, 2009 at 12:42
  • Also, welcome to stackoverflow :) Commented Dec 30, 2009 at 12:48

3 Answers 3

6

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.

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

Comments

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

Comments

0

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)

Comments

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.