最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Flask-Login出错:AttributeError Flask object has no attribute flash

Flask crifan 5182浏览 0评论

折腾:

[已解决]Flask-Login出错:AttributeError AnonymousUserMixin object has no attribute openid

之后,参考:

Flask-Login — Flask-Login 0.3.2 documentation

写的代码:

        gLog.info(“record logged in for user=%s”, curUser)
        login_user(curUser, remember = True) # 第一个参数传入用户对象,第二个参数 传入 以后是否自动登陆
        app.flash(u’用户 %s 登录成功’, curUser.nickname)

又出错:

[2016-09-01 20:46:06 +0000] [13500] [ERROR] Error handling request /wechat_auth?code=011HmcC71smP1X1oigC71UWaC71HmcCI&state=
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 135, in handle
    self.handle_request(listener, req, client, addr)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/sync.py”, line 176, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 2000, in __call__
    return self.wsgi_app(environ, start_response)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1991, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1567, in handle_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/flask/app.py”, line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File “/root/html/SIPEvents/sipevents/views.py”, line 683, in wechat_auth
    app.flash(u’用户 %s 登录成功’, curUser.nickname)
AttributeError: ‘Flask’ object has no attribute ‘flash’

搜:

AttributeError: ‘Flask’ object has no attribute ‘flash’

Flask   flash

消息闪现 — Flask 0.10.1 文档

Message Flashing — Flask Documentation (0.11)

好像是:

from flask import Flask, flash

然后用:

flash(‘You were successfully logged in’)

而此处的:

app是:

app = Flask(__name__, instance_relative_config=True)

所以去改为:

from flask import flash
flash(u’用户 %s 登录成功’, curUser.nickname)

就可以了。

-》不过此处没有继续去使用flask,因为还要模板html中获取对应的flash信息再显示。觉得麻烦。

[总结]

此处app.flash出错的原因是app是大写的Flask,而flash是小写的flask中的功能

所以报错。

解决办法是:

从flask导入flash,即可使用:

from flask import flash
flash(u’login OK’)

转载请注明:在路上 » [已解决]Flask-Login出错:AttributeError Flask object has no attribute flash

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.184 seconds, using 22.06MB memory