问题描述
我正在开发一个Flask项目,并且正在使用棉花糖来验证用户输入.下面是一个代码段:
I am working on a Flask project and I am using marshmallow to validate user input.Below is a code snippet:
def create_user():
in_data = request.get_json()
data, errors = Userschema.load(in_data)
if errors:
return (errors), 400
fname = data.get('fname')
lname = data.get('lname')
email = data.get('email')
password = data.get('password')
cpass = data.get('cpass')
当我删除errors
部分时,代码可以正常工作.按原样运行时,出现以下错误:
When I eliminate the errors
part, the code works perfectly. When I run it as it is, I get the following error:
ValueError:太多值无法解包(预期2)
ValueError: too many values to unpack (expected 2)
回溯(最近通话最近一次)
Traceback (most recent call last)
文件 "/home/..project-details.../venv3/lib/python3.6/site-packages/flask/app.py", 第2000行,在致电
File "/home/..project-details.../venv3/lib/python3.6/site-packages/flask/app.py", line 2000, in call
错误=无
ctx.auto_pop(错误)
ctx.auto_pop(error)
def __call__(self, environ, start_response):
"""Shortcut for :attr:`wsgi_app`."""
return self.wsgi_app(environ, start_response)
def __repr__(self):
return '<%s %r>' % (
self.__class__.__name__,
self.name,
注意:var in_data
是字典.有什么想法吗?
Note: The var in_data
is a dict.Any ideas??
推荐答案
我建议您检查依赖项版本.根据棉花糖API参考,schema.load返回:
I recommend you check your dependency versions.Per the Marshmallow API reference, schema.load returns:
我怀疑python正在尝试将dict(作为单个对象返回)解压缩为两个变量.引发异常是因为错误"变量中没有任何内容.下面再现了错误:
I suspect python is trying to unpack the dict (returned as a singular object) into two variables. The exception is raised because there is nothing to pack into the 'errors' variable. The below reproduces the error:
d = dict()
d['test'] = 10101
a, b = d
print("%s : %s" % (a, b))
这篇关于Python(烧瓶/棉花糖)ValueError:太多值无法解包(预期2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!