棉花糖webargs的parser.parse()始终返回None

时间:2020-05-19 03:54:48

标签: flask flask-restful webargs

在看到reqparse被弃用之后,我根据一些论坛上的建议切换到webargs进行查询参数解析和验证。但是,即使尝试使用quick start page中最基本的语法,也要使用@use_kwargs解析参数。有人可以看看我在下面的代码中是否做错了什么?也许我有错别字。

from pprint import pprint

from flask import request
from flask_restful import Resource
from webargs import fields, validate
from webargs.flaskparser import parser
website_args = {
    'type': fields.String(
        required=True,
        validate=validate.Range(min=0, max=6, error="testing error, never triggered")
    ),
}

# Flask-restful resource, GET method called with ?type=6
class WebsiteList(Resource):
    @classmethod
    @use_kwargs(website_args, location='query')
    def get(cls):
        # Line below outputs: ImmutableMultiDict([('type', '6')])
        pprint(request.args)

        # Line below works however, when manually parsing the arguments
        args = parser.parse(website_args, request, location='query')

使用@use_kwargs时(不是手动方式),我得到了TypeError: 'NoneType' object is not iterable的回溯。让我怀疑这是否是一个错误。

Traceback (most recent call last):
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\flask\app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\flask\app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\flask_restful\__init__.py", line 468, in wrapper
    resp = resource(*args, **kwargs)
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\flask\views.py", line 89, in view
    return self.dispatch_request(*args, **kwargs)
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\flask_restful\__init__.py", line 583, in dispatch_request
    resp = meth(*args, **kwargs)
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\webargs\core.py", line 364, in wrapper
    args, kwargs, parsed_args, as_kwargs
  File "C:\Users\Marco\.virtualenvs\api-kHflOUBu\lib\site-packages\webargs\core.py", line 298, in _update_args_kwargs
    kwargs.update(parsed_args)
TypeError: 'NoneType' object is not iterable

0 个答案:

没有答案