I have a list of items, each one with a checkbox to select it. I want
to post this info to a django view, but i don't know how should I use
the name="" and value="" of the input tag to gain access to it from
django as it if was an array.
I used something like that in PHP:
...
<input type="checkbox" name="array[<?php echo $id; ?>]" value="1"> Text
Text
...
So I was able to know what item's where selected very easy. Just make a
loop through the array post var, and check what id's have a "1" value.
I suppose there's an easy way to do something like that in django, but
don't know how.
Need some help.
Thanks in advance.
>I have a list of items, each one with a checkbox to select it. I want
>to post this info to a django view, but i don't know how should I use
>the name="" and value="" of the input tag to gain access to it from
>django as it if was an array.
>
>
Give them all the same name but different value.
<input type="checkbox" name="array" value="{{id}}">
Then when you submit a form browser will send values of checked
checkboxes and then in a view you can do
request.POST.getlist('myfield')
this'll give you normal Python list with values of all checked checkboxes.
> Give them all the same name but different value.
>
> <input type="checkbox" name="array" value="{{id}}">
>
> Then when you submit a form browser will send values of checked
> checkboxes and then in a view you can do
>
> request.POST.getlist('myfield')
A typo: I meant 'myfield' and 'array' to be the same name but messed
them up when editing :-)
I have a list of items, each one with a checkbox to select it. I want
to post this info to a django view, but i don't know how should I use
the name="" and value="" of the input tag to gain access to it from
django as it if was an array.
I used something like that in PHP:
...
<input type="checkbox" name="array[<?php echo $id; ?>]" value="1"> Text
Text
...
So I was able to know what item's where selected very easy. Just make a
loop through the array post var, and check what id's have a "1" value.
I suppose there's an easy way to do something like that in django, but
don't know how.
Need some help.
Thanks in advance.