代码之家  ›  专栏  ›  技术社区  ›  AlexVB

烧瓶安全:“当前用户”是从哪里来的?

  •  4
  • AlexVB  · 技术社区  · 7 年前

    这很可能是个愚蠢的问题,但我仍然找不到答案。

    Documentation

    如果您只需要在某些时候要求用户登录,您可以使用以下工具:

    if not current_user.is_authenticated:
      return current_app.login_manager.unauthorized()
    

    但它不知道这个“current_user”变量来自哪里。毫不奇怪,当我尝试在应用程序源代码中使用它时,我得到了一个“NameError:全局名称‘current_user’未定义”。

    奇怪的是,当我在HTML模板中使用变量时, render_模板

    1 回复  |  直到 7 年前
        1
  •  4
  •   Bahrom    7 年前

    你需要 import current_user .

    from flask_login import current_user
    

    至于它来自哪里-它来自不同的包, flask-login :

    #: A proxy for the current user. If no user is logged in, this will be an
    #: anonymous user
    current_user = LocalProxy(lambda: _get_user())