django MultiValueDictKeyError error, how do I deal with it

前端 未结 7 2057
终归单人心
终归单人心 2020-11-22 11:14

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<

7条回答
  •  花落未央
    2020-11-22 12:02

    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 MultiValueDictKeyError
    try:
        is_private = request.POST['is_private']
    except MultiValueDictKeyError:
        is_private = False
    

提交回复
热议问题