Python Flask:AttributeError:'NoneType'对象没有属性'is_active' [英] Python Flask: AttributeError: 'NoneType' object has no attribute 'is_active'

查看:82
本文介绍了Python Flask:AttributeError:'NoneType'对象没有属性'is_active'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Flask Mega Tutorial 我正在尝试学习Flask.在教程的第5部分中我现在正在建立一个配置文件页面,该页面需要用户登录.

Using the Flask Mega Tutorial I'm trying to learn Flask. In part 5 of the tutorial I'm now building a profile page which needs the user to be logged in.

由于我使用的是peewee ORM而不是SQLAlchemy,所以我确实在这里和那里调整了代码,但是对于这种情况,我认为这并不重要.我现在使用我的Google(openID)帐户登录,并遇到一条错误消息,提示 AttributeError:'NoneType'对象没有属性'is_active'.该函数的结尾处出现错误,该行显示 login_user(user,Remember = Remember_me).

Since I'm using the peewee ORM instead of SQLAlchemy, I do adjust the code here and there, but for this case I don't think that matters. I now login using my Google (openID) account and hit an error saying AttributeError: 'NoneType' object has no attribute 'is_active'. The error occurs at the end of this function at the line which says login_user(user, remember = remember_me).

@oid.after_login
def after_login(resp):
    if resp.email is None or resp.email == "":
        flash('Invalid login. Please try again.')
        return redirect(url_for('login'))

    user = User.select().where(User.email == resp.email).first()

    if user is None:
        nickname = resp.nickname
        if nickname is None or nickname == "":
            nickname = resp.email.split('@')[0]
        User(nickname = nickname, email = resp.email, role = models.ROLE_USER, last_seen = datetime.utcnow()).save()

    remember_me = False
    if 'remember_me' in session:
        remember_me = session['remember_me']
        session.pop('remember_me', None)
    login_user(user, remember = remember_me)
    return redirect(request.args.get('next') or url_for('index'))

由于这段代码之前已经起作用,所以我不知道为什么现在会出现错误. is_active 方法(非属性)出现在User类中,如下所示:

Since this piece of code worked before, I wouldn't know why this now gives an error. The is_active method (not attribute) occurs in the User class, which looks like this:

class User(db.Model):
    nickname = CharField()
    email = CharField(max_length=150)
    role = IntegerField(default = ROLE_USER)
    about_me = TextField(null = True, max_length=140)
    last_seen = DateTimeField()

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    def avatar(self, size):
        return 'http://www.gravatar.com/avatar/' + md5(self.email).hexdigest() + '?d=mm&s=' + str(size)

    def __repr__(self):
        return '<User %r>' % (self.nickname)

但是我有点迷茫,为什么这会让我出错.所以我的问题确实是:有人知道我在这里做错什么吗?

But I'm kinda lost as to why this would throw me an error. So my question is really: does anybody know what I' doing wrong here?

欢迎您提出任何建议,因为我完全迷路了.

Any tips are welcome, since I'm totally lost..

推荐答案

程序段中无位置

if user is None:

您是否确实将任何东西分配 user ,所以仍然是 None ,并且您会收到错误消息.尝试更改为:

do you actually assign anything to user, so it is still None and you get the error. Try changing to:

user = User(nickname = nickname, ...)

或致电

user = User.select().where(User.email == resp.email).first()

再次在 if

这篇关于Python Flask:AttributeError:'NoneType'对象没有属性'is_active'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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