django MultiValueDictKeyError error, how do I deal with it
Choose what is best for you:
1
is_private = request.POST.get('is_private', False);
If is_private
key is present in request.POST the is_private
variable will be equal to it, if not, then it will be equal to False.
2
if 'is_private' in request.POST: is_private = request.POST['is_private']else: is_private = False
3
from django.utils.datastructures import MultiValueDictKeyErrortry: is_private = request.POST['is_private']except MultiValueDictKeyError: is_private = False
You get that because you're trying to get a key from a dictionary when it's not there. You need to test if it is in there first.
try:
is_private = 'is_private' in request.POST
or
is_private = 'is_private' in request.POST and request.POST['is_private']
depending on the values you're using.