[Django]-How to get checkbox value in django?

2👍

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.

6👍

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"
👤Mp0int

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.

-1👍

As others stated, this is simle html – You have to put your foo and bar values on the “value” attribute of the input tag, regardless of what is shown on the page. And that is all you need to do.

<tr name="item">
        <td><input type="checkbox" name="item" value="{{foo}},{{bar}}"></td>
        <td>>{{ foo }}</td>
        <td> {{ bar }} </td>


</tr>

Leave a comment