Flask:已经登录后如何防止进入登录页面? [英] Flask: How to prevent to go to login page when already logged in?
问题描述
我一直在编写Miguel Grinberg编写的Flask Web开发手册,最后完成了授权蓝图.但是,我有一个问题,如果我已经登录,我仍然可以输入登录页面的URL并转到那里.如何在Flask中防止这种情况?
I have been doing the Flask Web Development Book by Miguel Grinberg, and just finished up with the authorization blueprint. Though, I am having an issue that if I am already logged in, I can still put the url of the login page and go there. How can I prevent this in Flask?
在使用Django时,我想出了django-braces库来帮助实现这一点,Flask中是否提供了此类替代方法?
While using Django, I came up with django-braces library which helps to do this, any such alternative available in Flask?
推荐答案
重定向已登录导航到登录页面的用户.
Redirect users who are logged in navigating to the login page.
要检查它们是否已经登录,请检查 current_user
代理.请注意,已登录的用户将具有 current_user.is_authenticated()
是的,虽然未登录的用户将使该方法返回False.
To check if they are already logged in, examine the current_user
proxy. Note that logged-in users will have current_user.is_authenticated()
equal True, while users who aren't logged in will have the method return False.
对于目的地,您有两个选择:使用redirect(request.referrer)
返回到他们的上一页,或者使用redirect(url_for('view_name'))
返回到其他页面之一.
You have two options for the destination: back to their previous page with redirect(request.referrer)
or to one of your other pages with redirect(url_for('view_name'))
.
这篇关于Flask:已经登录后如何防止进入登录页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!