将Flash消息类别添加到@login_redirected重定向

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

我正在使用Python / Flask构建Web应用程序。我使用@login_required装饰器保护视图。如果用户未登录,它将重定向到我的“登录”视图URL,并添加一条带有(“您需要登录才能查看此页面。”)的即兴消息。加载重定向视图(“登录”)后,将显示此Flash消息。由于该邮件看起来来自@login_required,因此如何为该邮件添加类别?

这是我的视图功能:

@app.route('/')
@app.route('/index')
# This is the decorator that redirects my request to the
# 'login' view, and adds a flash message. 
@login_required
#
def index():
    ...
    return render_template(...)

为了将类别传递给即时消息,我可以“手动”添加,如下所示。

flash_message = flash("User needs to be logged in to view this page", "category")
python flask flask-login
1个回答
0
投票

我刚刚遇到了这个问题,因为我遇到了同样的问题。您可能已经知道了,但是您需要添加以自定义@login_required的是

login_manager.login_view = "users.login"

login_manager.login_message = "User needs to be logged in to view this page"

login_manager.login_message_category = "warning"
© www.soinside.com 2019 - 2024. All rights reserved.