django MultiValueDictKeyError错误,我该如何处理它 [英] django MultiValueDictKeyError error, how do i deal with it
问题描述
问题在于形式,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屋!