Suman Chabria
Suman Chabria

Reputation: 3

For a checkbox with multiple values in my html template how to access it through python code?

I'm a python and django newbie.

This is in my html template

<input type ="checkbox" value={{ item.id }} name="ck1[]"> 

In views.py when i do a checked = request.POST.get(['ck1']) i get unhasable list error. Kindly guide me.

Upvotes: 0

Views: 1034

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599610

Please don't use PHP syntax when you're writing Django. name="ck1[]" is a PHP-ism that's completely unnecessary.

If you want the field to be called ck1, just call use name="ck1", and use `request.POST.getlist('ck1') in your view.

If you really have to use that horrible bracket syntax, you'll need to use request.POST.getlist('ck1[]'), because Django quite sensibly believes that the name you use in the HTML is the name you should get in the POST data.

Upvotes: 1

kartheek
kartheek

Reputation: 6684

If u want to get array from html in Django view u need to use

checked = request.POST.getlist('ck1[]')

or

checked = request.POST.getlist('ck1')

getlist method will convert all selected values into python list

Upvotes: 0

Related Questions