django后复选框数据 [英] django post checkbox data

查看:118
本文介绍了django后复选框数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的模板中使用了一个复选框,在我看来,我试图检查是否已经选中了这个框
我的视图中有以下代码:

  if request.POST ['check'] == True:

,但如果未选中,则会抛出错误。我如何检查我的帖子数据中是否有值检查?



谢谢

解决方案

Python文档是您的朋友:

 如果request.POST.get('check',False):
...做东西...

你也可以这样做(更多Python文档):

 如果请求中检查.POST:
...做东西...


I'm using a checkbox form in my template and in my view im trying to check if the box has been checked or not I have the following code in my view:

if request.POST['check'] == True:

but then it throws an error if it is unchecked. How do i check if there is a value 'check' in my post data?

Thanks

解决方案

The Python docs are your friend:

 if request.POST.get('check', False):
     ...do stuff...

You could also do this (more Python docs):

 if "check" in request.POST:
     ... do stuff...

这篇关于django后复选框数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆