【发布时间】:2023-02-23 00:50:50
【问题描述】:
我正在尝试制作一个带有登录系统的网络应用程序。我想让它使用户无法访问某些页面,除非他们已登录。
我想要的是,当您在未登录的情况下单击转到另一个页面时,您将被重定向到登录页面,并在其上收到一条消息。
这是有效的:
@app.route("/home", methods=['GET', 'POST'])
def home():
#some form
if not current_user.is_authenticated:
flash('You need to be logged in to access this page.', 'info')
return redirect(url_for('login'))
#rest of the code
但我还需要将所有这些添加到其他路线。所以我创建了函数并将其添加到路由中:
@app.route("/home", methods=['GET', 'POST'])
def home():
#some form
require_login()
#rest of the code
def require_login():
if not current_user.is_authenticated:
flash('You need to be logged in to access this page.', 'info')
return redirect(url_for('login'))
但这并不像我想要的那样工作。它而是重定向到主页,然后闪烁消息。我该如何解决?
【问题讨论】:
标签: python flask flask-login