Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the values of all selected checkboxes in a Django request.POST?

Hi I have an array of checkboxes e.g.

<input type="checkbox" name="checks[]" value="1" /> <input type="checkbox" name="checks[]" value="2" /> <input type="checkbox" name="checks[]" value="3" /> <input type="checkbox" name="checks[]" value="4" /> 

How do I access these in the view.py if more than one is selected?

I have tried

request.POST['checks'] 

but that only gives me the last value. What I want is all the ones that have been selected in a list e.g. 1,3,4

Thanks

like image 745
John Avatar asked Mar 10 '10 13:03

John


People also ask

How do you check checkbox is checked or not in Django?

POST or None) if request. method == "POST": if form. is_valid(): ... if request. POST["something_truthy"]: # Checkbox was checked ...


1 Answers

Try this:

some_var = request.POST.getlist('checks') 

some_var will contain [1,3,4] (those values that were checked)

like image 62
Silver Light Avatar answered Sep 28 '22 00:09

Silver Light