在Flask-Login文档中,它描述了系统用户如何需要经过身份验证的用户模型来访问使用装饰器语法的方法:
from flask_login import login_required @app.route("/settings") @login_required def settings(): pass
现在这一切都很好,但我希望能够检查用户是否登录了一个方法,如下所示:
@app.route('/main/', methods=['GET', 'POST']) main_route(): if request.method == 'GET': if user_is_authenticated(): #Do the authentication here #load authenticated /main/ html template etc. pass else: #load unauthenticated /main/ html template etc. pass ...
这样做的原因是因为它将GET和POST请求分解,而不是为经过身份验证的用户和未经身份验证的用户复制路由.
我怎样才能做到这一点?可能吗?
烧瓶非常简单:
from flask_login import current_user @app.route(...) def main_route(): if current_user.is_authenticated: return render_template("main_for_user.html") else: return render_template("main_for_anonymous.html")
请参阅匿名用户的文档.