如何修复AttributeError:在Flask中使用Decorator时,类型对象'Wrapper'没有属性'as_view'

时间:2019-09-24 03:05:00

标签: python flask decorator

我写了一些关于网站的烧瓶代码。它可以正常工作。 我想知道用户访问的统计信息。所以我用python编写了装饰器代码。当我在我的烧瓶代码中使用装饰器时。它告诉我错误: AttributeError:类型对象“ Wrapper”没有属性“ as_view”

在烧瓶中使用装饰器代码时,如何解决“ as_view”错误?

装饰代码

def Trace(cls):
    class Wrapper:
        def __init__(self, *args, **kwargs):
            print("log something")
            self.wrapped = cls(*args, **kwargs)
        def __getattr__(self, attrname):
            return getattr(self.wrapped, attrname)
    return Wrapper

烧瓶代码

@Trace
class UserInfo(Resource):
    def post(self):
        parser.add_argument('query_type', type=str, help='query_type should not empty')
        parser.add_argument('query_value', type=str, help='query_type should not empty')
        args = parser.parse_args()
        query_type = args['query_type']
        query_value = args['query_value']
        return get_user_info(query_type, query_value)

Traceback (most recent call last):
  File "/Users/leo/Documents/code/python-code/flask-newbrand-platform-tool/flask-newbrand-platform-tool.py", line 17, in <module>
    api.add_resource(UserInfo, '/user_info')
  File "/Users/leo/anaconda3/envs/py3.6/lib/python3.6/site-packages/flask_restful/__init__.py", line 404, in add_resource
    self._register_view(self.app, resource, *urls, **kwargs)
  File "/Users/leo/anaconda3/envs/py3.6/lib/python3.6/site-packages/flask_restful/__init__.py", line 444, in _register_view
    resource_func = self.output(resource.as_view(endpoint, *resource_class_args,
AttributeError: type object 'Wrapper' has no attribute 'as_view'

0 个答案:

没有答案