Flask Abort(403) Forbidden 错误消息说是Forbidden,不让我调用它

问题描述 投票:0回答:2

我正在与用户创建一个博客,并向某些页面添加身份验证,要求登录的用户是管理员才能访问某些页面。下面是检查用户是否是管理员的修饰函数:

def admin_only(f):
    @wraps(f)
    def decorated_function(*args, **kwargs):
        try:
            user_id = current_user.id
        except:
            user_id = 0

        if user_id == 1:
            return f(*args, **kwargs)
        else:
            return abort(403)

    return decorated_function()

我收到以下错误消息:

werkzeug.exceptions.Forbidden: 403 Forbidden: You don't have the permission to access the requested resource. It is either read-protected or not readable by the server.

我已经从烧瓶中导入了中止,所以不能100%确定为什么它说它是禁止的。任何提示,提前谢谢!

python flask werkzeug
2个回答
1
投票

我相信应该是

abort(403)
而不是
return abort(403)

abort 将为给定的状态代码 (403) 引发异常,并且异常会隐式返回


0
投票

尝试从最终返回的修饰函数中删除括号。让它

return decorated_function
而不是
return decorated_function()

© www.soinside.com 2019 - 2024. All rights reserved.