小编典典

使Flask-Login的login_required为默认值的最佳方法

flask

我现在使用Flask-Loginlogin_required装饰器。无论如何,使其成为默认行为Flask


阅读 735

收藏
2020-04-06

共1个答案

小编典典

我在仪器项目中做到了这一点。我使用before_request装饰器:

@app.before_request
def check_valid_login():
    login_valid = 'user' in session # or whatever you use to check valid login

    if (request.endpoint and 
        'static' not in request.endpoint and 
        not login_valid and 
        not getattr(app.view_functions[request.endpoint], 'is_public', False) ) :
        return render_template('login.html', next=request.endpoint)      

然后,我is_public()为无需登录即可访问的几个地方创建了一个装饰器:

def public_endpoint(function):
    function.is_public = True
    return function
2020-04-06