How to use login_required with a class, in Flask?

2019-08-06 18:32发布

from flask_login import login_required
from flask_restful import Resource

@login required
class MyClass(Resource):
    #...

In the main file I call the class's methods like:

api.add_resource(MyClass, '/some_url', methods=['GET', 'PUT', 'POST', 'DELETE'])

I think I am using @login_required the wrong way here, since I get the error AttributeError: 'function' object has no attribute 'as_view'

So I am assuiming that @login_required can only be used with functions. Is there a way to incorporate it with a class?

1条回答
祖国的老花朵
2楼-- · 2019-08-06 19:26

you could give the class the decorators it should run :

class MyClass(Resource):
    decorators = [login_required]
查看更多
登录 后发表回答