flask before request - add exception for specific route

前端 未结 3 989
忘掉有多难
忘掉有多难 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条回答
  •  -上瘾入骨i
    2020-12-04 21:56

    There are a couple of properties on the request object you can check, documented here, request.path is probably what you want. Can I suggest request.endpoint though, so you'll be covered should you decide to route your view to another url, or multiple urls

    @app.before_request
    def before_request():
        if 'logged_in' not in session and request.endpoint != 'login':
            return redirect(url_for('login'))
    

提交回复
热议问题