python - 这如何解决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
解决方案
在这里你使用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
所以如果你通过电子邮件它会通过电子邮件其他明智的默认值电子邮件未找到
希望你明白我想说的可能回答对你的问题感到满意
推荐阅读
- node-red - Watson Assistant NodeRed 节点不接受新的 IAM 安全凭证
- angular - 角度材料选项卡高度
- javascript - Angular-tree-component 不显示任何节点
- ios - 使用 OHHTTPStubs 模拟时未达到 Alamofire 响应块
- python - Python 3: vehicle inventory using class
- javascript - 将项目移动到不同位置后的淘汰排序更新功能
- android - 在 Android 的 volley 中使用 Get 方法向 Spring 框架(MVC)发送请求
- go - 如何找到 go 包的版本
- c - 这个 char* 任务发生了什么?(混合类型的逗号运算符)
- java - 使用 Apache FOP 生成的 PDF 为空