current_user.is_authenticated()上的AttributeError [英] AttributeError on current_user.is_authenticated()

查看:42
本文介绍了current_user.is_authenticated()上的AttributeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Flask-Login,我想通过调用current_user.is_authenticated()来检查用户是否已登录.

I'm using Flask-Login and I want to check if a user is logged-in by calling current_user.is_authenticated().

我的Flask应用程序通过一个主要的 __ init __.py 文件组织成一个蓝图,该文件用于设置我的应用程序,设置蓝图并初始化Flask-Login.我有一个脚本 run.py ,位于 __ init __.py 的一个目录中,该脚本实例化该应用程序并在调试模式下运行它.

My Flask application is organized into Blueprints with a main __init__.py file that sets up my app, sets up Blueprints, and initializes Flask-Login. I have a script, run.py, located up one directory from __init__.py that instantiates the application and runs it if in debug mode.

__ init.py __ 的外观(部分)如下:

__init.py__ looks (in part) like this:

from flask import Flask, session, request, render_template                                                            
from flask.ext.login import LoginManager, current_user

from panel.views import dash, projects

from panel.users.models import User, AnonymousUser

app = Flask(__name__)
app.config.from_object("configure") 

login_manager = flask_login.LoginManager()
login_manager.init_app(app)
login_manager.login_view = "/signin"
login_manager.anonymous_user = AnonymousUser
@login_manager.user_loader
def load_user(str_id):
    return User.user_loader(str_id)

# Register all blueprints here.
app.register_blueprint(dash.bp, url_prefix="/dash")
app.register_blueprint(projects.bp, url_prefix="/projects")

logging.warning(current_user.is_authenticated())

当我运行此代码时,从 run.py (部分看起来像这样)中进行

When I run this, from run.py (which looks, in part, like this):

from panel import app 

app.run(host='0.0.0.0', debug=True)

引发此异常:

Traceback (most recent call last):
  File "run.py", line 5, in <module>
    from panel import app
  File "/opt/www/panel/panel/__init__.py", line 40, in <module>
    logging.warning(current_user.is_authenticated())
  File "/opt/virtualenvs/coefficient/lib/python2.6/site-packages/werkzeug/local.py", line     336, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/opt/virtualenvs/coefficient/lib/python2.6/site-packages/werkzeug/local.py", line 295, in _get_current_object
    return self.__local()
  File "/opt/virtualenvs/coefficient/lib/python2.6/site-packages/flask_login.py", line 403, in <lambda>
    current_user = LocalProxy(lambda: _request_ctx_stack.top.user)
AttributeError: 'NoneType' object has no attribute 'user'

因为简洁起见,我省略了一些行,所以该回溯中的行号与我发布的代码不匹配,但是您可以看到行 logging.warning(current_user.is_authenticated())是发生错误的地方.

Because I omitted some lines for brevity, the line numbers in that traceback do not match the code I posted, but you can see that the line logging.warning(current_user.is_authenticated()) seems to be where the error occurs.

感谢您的帮助.

推荐答案

您无法致电

current_user.is_authenticated()

方法/视图之外,因为方法/视图之外的代码将在请求之外运行.

outside of a method/view, because the code outside of the methods/views will be run outside of a request.

如果您不在请求中,则无需检查身份验证的 current_user .

If you're not in a request, there is no current_user on which to check authentication.

这篇关于current_user.is_authenticated()上的AttributeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆