how to get checkbox value in django?

10,495

Solution 1

I'm not sure to understand everything you asked, but if you want to get "foo" and "bar" when the user submit the form, you will have to add them in a form element like hidden or textfields (depending on if the user can modify them or not).

The server will never receive the whole DOM when you submit a form.

Also, you will have to find a way to indicate on which checkbox belongs foo & bar.

Solution 2

Checkboxes work a little bit different from other form inputs, so if you examine a post sent from a form that includes a checkbox, there are two possibilities...

<input type="checkbox" name="cb1" />

if the checkbox is checked, your queryset will look like:

queryset = {'cb1': 'on'}

İf it is not checked:

queryset = {}

So, you have to check the existence of the related form element name:

if 'cb1' in queryset: 
    "item is selected"
else:
    "item is not selected"

Solution 3

Your question is nonsensical. foo and bar are not checkbox values - they appear to be simple text elements within a table. If you want those posted as values, you'll need to put them into a form somewhere. This is nothing to do with Django.

Share:
10,495
xasjaiod123
Author by

xasjaiod123

Updated on July 28, 2022

Comments

  • xasjaiod123
    xasjaiod123 almost 2 years
    <tr name="item">
            <td><input type="checkbox" ></td>
            <td>>{{ foo }}</td>
            <td> {{ bar }} </td>
    
    
    </tr>
    

    Now I want that in django views request.POST.getlist('item') returns the value of ie foo and bar. But it is returning null.