AttributeError:'用户'对象没有属性' is_active'

时间:2014-12-29 21:03:28

标签: flask flask-sqlalchemy flask-login

我尝试运行我的应用程序进行登录但是当我提交表单以检查用户ID和电子邮件时我收到此错误。属性错误:'用户'对象没有属性'is_active'所以,谢谢你的帮助

   rv = self.dispatch_request()

    File "/home/peg/flask/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request

    return self.view_functions[rule.endpoint](**req.view_args)

    File "/home/peg/flask/app/view.py", line 45, in login

    login_user(user_in_db, remember = form.remember_me.data)

    File "/home/peg/flask/venv/lib/python2.7/site-packages/flask_login.py", line 675, in login_user

    if not force and not user.is_active():
  

view.py

@app.route('/login', methods = ['GET', 'POST'])
def login():
    form = LoginForm()
    #checks if the user is authernticated
    #or not, if yes it skips authentfic.
    if current_user is not None and current_user.is_authenticated():
            return redirect(request.args.get('next') or url_for('index'))
    #does not allow user to use get method
    if request.method == 'GET':
            return render_template('login.html',
                    form = form,
                    title = 'Login')

    #taking the user submitted data and checking if it exists in the database
    user_in_db = User.query.filter_by(name=form.name.data.lower()).first()

    #if the username is not wrong
    if user_in_db is not None and user_in_db != False:
            if form.email.data !=  user_in_db.email:
                    flash('email is incorrect')
                    return redirect(url_for('login'))
            #here I got error
            login_user(user_in_db, remember = form.remember_me.data)
            return redirect(request.args.get('next') or url_for('index'))
    else:
       flash('Username does not exists')
       return render_template('login.html',
            form = form,
            title = 'Login')

model.py

class User(db.Model):
   __tablename__ = "Contacts"
   id = db.Column(db.Integer, primary_key = True)
   name = db.Column(db.String(50))
   email = db.Column(db.String(50))
   age = db.Column(db.Integer)

def __init__(self, name, email, age ):
    self.name = name
    self.email = email
    self.age = age

def is_authenticated(self):
    return True

def is_active(self):
    return True

def is_anonymous(self):
    return False

def get_id(self):
    return unicode(self.id)


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

1 个答案:

答案 0 :(得分:0)

你的缩进是错误的。您没有在User上定义方法,而是定义了模块级函数。

class User(db.Model):
    __tablename__ = "Contacts"
    id = db.Column(db.Integer, primary_key = True)
    name = db.Column(db.String(50))
    email = db.Column(db.String(50))
    age = db.Column(db.Integer)

    def __init__(self, name, email, age ):
        self.name = name
        self.email = email
        self.age = age

    def is_authenticated(self):
        return True

    def is_active(self):
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return unicode(self.id)

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