问题
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:
you could give the class the decorators it should run :
class MyClass(Resource):
decorators = [login_required]
来源:https://stackoverflow.com/questions/46932366/how-to-use-login-required-with-a-class-in-flask