flask before request - add exception for specific route

前端 未结 3 1000
忘掉有多难
忘掉有多难 2020-12-04 20:56

In the before_request() function (below), I want to redirect the user to /login if they are not yet logged in. Is there a special variable that wil

3条回答
  •  青春惊慌失措
    2020-12-04 22:02

    Here's an implementation of the accepted answer with flask-login:

    @app.before_request
    def require_authorization():
        from flask import request
        from flask.ext.login import current_user
    
        if not (current_user.is_authenticated or request.endpoint == 'login'):
            return login_manager.unauthorized()
    

提交回复
热议问题