带有装饰器的 flask MethodView 给出错误

flask MethodView with decorators is giving error

也许我做错了什么。我正在尝试将 flask-security-too auth_required 与烧瓶的 MethodView 一起使用,如 https://flask.palletsprojects.com/en/1.1.x/views/#decorating-views 所述,当我尝试访问该页面时出现异常。 python3.6.8

from flask_security import auth_required

class RunningRoutesAdmin(MethodView):
    decorators = [auth_required]

    def get(self):
        return render_template('admin.jinja2',
                               pagename='Admin Home')

admin_view = RunningRoutesAdmin.as_view('admin')
app.add_url_rule('/admin/', view_func=admin_view, methods=['GET',])

异常

Traceback (most recent call last):
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 2463, in __call__
    return self.wsgi_app(environ, start_response)
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 2449, in wsgi_app
    response = self.handle_exception(e)
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 1866, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\_compat.py", line 39, in reraise
    raise value
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "C:\Users\lking\Documents\Lou's Software\projects\runningroutes\runningroutes\venv\lib\site-packages\flask\app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
TypeError: wrapper() missing 1 required positional argument: 'fn'

好的 - 简单的解决方案 - 因为 auth_required 需要参数 - 你需要:

decorators = [auth_required()]

抱歉 - 我建议传入 None - 这当然不正确。不传递任何内容或传递可接受的方法列表(例如 "session"、"token")都可以正常工作。