how to get checkbox value in django?

喜夏-厌秋 提交于 2019-12-05 22:31:58

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.

FallenAngel

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"

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.

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>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!