首页 > 解决方案 > 这如何解决python 3.4中的这个multivaluedictkeyerror?

问题描述

@api_view(['GET', ])
@permission_classes([])
@authentication_classes([])
def does_account_exist_view(request):

    if request.method == 'GET':
        check_mail = request.GET['email'].lower()
        print(check_mail)
        data = {}
        try:
            account = Account.objects.get(email=check_mail)
            data['response'] = check_mail
        except Account.DoesNotExist:
            data['response'] = "Account does not exist"
        return Response(data)

这段代码在 python 3.8.5 中运行良好,但在我的服务器 python 3.4.3 上为什么会出现这样的错误?

/api/account/check_if_account_exists 'email' 处的 MultiValueDictKeyError

请求方法:GET 请求 URL: http: //127.0.0.1 :8000/.../.../check_if_account_exists Django 版本:2.0.13 Python 可执行文件:C:\p3.4_envs\Scripts\python.exe Python 版本: 3.4.3

标签: pythondjango

解决方案


在这里你使用GET方法,它看起来像 dict,但它是标准的 dict。如果您想消除此错误,则提供了一种方法是get it call on dict 并且它接受参数首先是 dict 的键,如果在GET中找不到键,则第二个是默认值

在这里您可以查看 get 方法是否具有电子邮件键,它将返回其他默认值

 check_mail = request.GET.get('email','email not found')
 print(check_mail)

确保电子邮件应该在 url 中,如http://127.0.0.1:8000/.../.../check_if_account_exists?email=admin@admin.com

所以如果你通过电子邮件它会通过电子邮件其他明智的默认值电子邮件未找到

获取dict的方法

希望你明白我想说的可能回答对你的问题感到满意


推荐阅读