Asked By – dotty
I’m trying to save a object to my database, but it’s throwing a
The problems lies within the form, the
is_private is represented by a checkbox. If the check box is NOT selected, obviously 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']
Now we will see solution for issue: django MultiValueDictKeyError error, how do I deal with it
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)
my_var = dict.get(<key>, <default>)