开发者

how to get checkbox value in django?

<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.


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.


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜