PYTHON - 烧瓶:'用户'对象没有属性' is_active'

时间:2014-12-22 12:58:42

标签: python flask

我遇到了登录烧瓶的问题。我在网站上搜索过,但我找不到解决方案...

当我想要连接时,我收到了这条消息:

AttributeError的

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

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__

return self.wsgi_app(environ, start_response)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app

response = self.make_response(self.handle_exception(e))

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception

reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app

response = self.full_dispatch_request()

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request

rv = self.handle_user_exception(e)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception

reraise(exc_type, exc_value, tb)

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request

rv = self.dispatch_request()

File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request

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

File "/home/ya2s/UNIVERSITE/WEB/web-serveur/projetvin/tuto/views.py", line 112, in login

login_user(inscrit)

File "/usr/local/lib/python2.7/dist-packages/flask_login.py", line 675, in login_user

if not force and not user.is_active():

AttributeError: 'User' object has no attribute 'is_active'

我在视图中的代码:

from wtforms import PasswordField
from .models import User
from hashlib import sha256

class LoginForm(Form):
    username = StringField('Username')
    password = PasswordField('Password')

    def get_authenticated_user(self):
        user = User.query.get(self.username.data)
        if user is None :
            return None
        m = sha256()
        m.update(self.password.data.encode())
        passwd = m.hexdigest()
        return user if passwd == user.password else None

from flask.ext.login import login_user, current_user
from flask import request

@app.route("/login/", methods=("GET","POST",))
def login():
    f = LoginForm()
    if f.validate_on_submit():
        inscrit = f.get_authenticated_user()
        if inscrit is not None : 
            login_user(inscrit)
            return redirect(url_for("home"))
    return render_template(
        "login.html",
        form = f)

我的模特:

from flask.ext.login import UserMixin
from app import login_manager

class User(db.Model):
    username = db.Column(db.String(50), primary_key=True)
    password = db.Column(db.String(64))

    def get_id(self):
        return self.username

from .app import login_manager

@login_manager.user_loader
def load_user(username):
    return User.query.get(username)

3 个答案:

答案 0 :(得分:4)

您的User课程没有定义任何方法is_active()。您的用户类需要实现这些documented methods

根据您的导入判断,您似乎已经打算这样做,但您可以使用UserMixin类为所需的User方法提供默认实现。您应该只能将UserMixin添加为User类的基类:

class User(db.Model, UserMixin):
    username = db.Column(db.String(50), primary_key=True)
    password = db.Column(db.String(64))

    def get_id(self):
        return self.username

答案 1 :(得分:1)

您的User类缺少flask-login期望的方法。 Flask-login提供了一个UserMixin,您正在导入但不使用它。

class User(db.Model):更改为class User(db.Model, UserMixin):。您可以阅读预期的方法in the docs

答案 2 :(得分:0)

以下是Flask-Login的示例实现,它帮助我解决了所有疑问:Flask-admin,请查看第37行,其中is_active()是开发人员定义的函数。

以下是我所指的快照:

# Create user model.
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(100))
    last_name = db.Column(db.String(100))
    login = db.Column(db.String(80), unique=True)
    email = db.Column(db.String(120))
    password = db.Column(db.String(64))

    # Flask-Login integration
    def is_authenticated(self):
        return True

    def is_active(self): # line 37
        return True

    def is_anonymous(self):
        return False

    def get_id(self):
        return self.id

    # Required for administrative interface
    def __unicode__(self):
        return self.username