How to use login_required with a class, in Flask?

余生长醉 提交于 2019-12-24 08:19:01

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!