django MultiValueDictKeyError错误,我该如何处理它 [英] django MultiValueDictKeyError error, how do i deal with it

查看:110
本文介绍了django MultiValueDictKeyError错误,我该如何处理它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

干草,我试图保存一个对象到我的数据库,但它抛出一个MultiValueDictKeyError错误。



问题在于形式,is_private被表示通过一个复选框。如果未选中该复选框,则无效通过。这是错误的地方。



我如何正确处理这个异常,并抓住它?



行是

  is_private = request.POST ['is_private '] 

感谢

解决方案

使用MultiValueDict的 get 方法。这也适用于标准类别,如果不存在,则提供默认值的方法。

  is_private = request.POST.get('is_private',False)

通常,

  my_var = dict.get(< key>,< default>)


Hay, I'm trying to save a object to my database, but it's throwing a MultiValueDictKeyError error.

The problems lies within the form, the is_private is represented by a checkbox. If the check box is NOT selected, obvously nothing is passed. This is where the error gets chucked.

How do i properly deal with this exception, and catch it?

The line is

is_private = request.POST['is_private']

Thank

解决方案

Use the MultiValueDict's get method. This is also present on standard dicts and is a way to fetch a value while providing a default if it does not exist.

is_private = request.POST.get('is_private', False)

Generally,

my_var = dict.get(<key>, <default>)

这篇关于django MultiValueDictKeyError错误,我该如何处理它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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