Skip to main content
 首页 » 编程设计

python之如何知道是否在 python之django 中选中了复选框

2025年05月04日26落叶无声

如果我有以下 html:

<form method ='GET' action='/search'> 
<input type='checkbox' name='box1' id='box1'> Option 1 
<input type='checkbox' name='box2' id='box2'> Option 2 
<input type='submit'> 
</form> 

我怎么知道在 python/django 中是否选中了其中一​​个复选框(TRUE/FALSE)?我正在用 Django 编写应用程序,我想根据选中的复选框显示特定结果。

非常感谢!

请您参考如下方法:

request.GET中如果存在则检查,不存在则不检查。

看到这个问题:Does <input type="checkbox" /> only post data if it's checked?

编辑:示例检查:

if 'box1' in request.GET: 
    # checked! 
    ... 
else 
    # not checked (or the form was never submitted.)! 
    ...