求助:flask login_user提示'int' object is not callable” 5C

Flask在登录时,提示'int' object is not callable,请各位帮助看一下是什么问题?
根据traceback最后一部提示在getid时出现问题,源码解释要返回unicode id,不知是不是这个原因?

错误提示:

   File "C:\Personal\Peoplefinder\venv\lib\site-packages\flask\app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "C:\Personal\Peoplefinder\Routes.py", line 66, in login
    login_user(user)
  File "C:\Personal\Peoplefinder\venv\lib\site-packages\flask_login\utils.py", line 161, in login_user
    user_id = getattr(user, current_app.login_manager.id_attribute)()
TypeError: 'int' object is not callable

Taceback源码提示:

 #: The default attribute to retreive the unicode id of the user
ID_ATTRIBUTE = 'get_id'

以下是代码:

Routes.py

 from flask import (
    render_template,
    request,
    redirect,
    flash,
    url_for,
    Flask
)
from Models import User
from flask_login import LoginManager, login_user, logout_user , current_user , login_required
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import create_engine, MetaData

import os

app = Flask(__name__)

path = os.path.dirname(__file__)
path = path + '\data\data.db'

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///{}'.format(path)
app.config['SECRET_KEY'] = 'kdije29fuk_ksdusii2w'

# engine = create_engine('sqlite:///' + path, echo=True)
# metadata = MetaData(engine)
**_**
db = SQLAlchemy(app)

login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = 'login'
login_manager.session_protection = 'strong'


@app.route('/register', methods=['GET', 'POST'])
def register():
    if request.method == 'GET':
        return render_template('register.html')
    user = User(request.form['username'], request.form['password'])
    db.session.add(user)
    db.session.commit()
    flash('User successfully registered')
    return redirect(url_for('login'))


@app.route('/login', methods=['GET', 'POST'])
def login():

    if request.method == 'GET':
        return render_template('login.html')
    username = request.form['username']
    password = request.form['password']
    user = User.query.filter_by(username=username).first()
    print(user)
    t = user.verify_password(password)
    print(t)

    if user is not None and t is True:
        login_user(user)
        print('成功')
        return redirect(url_for('login'))

    else:
        flash('Username or Password is invalid', 'error')
        print('失败')
        return redirect(url_for('register'))


if __name__ == '__main__':
    config = dict(
        debug=True,
        host='0.0.0.0',
        port=2000,
    )
    app.run(**config)

models.py

from flask_login import UserMixin, login_manager, LoginManager
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash

db = SQLAlchemy()


class User(db.Model, UserMixin):
    __tablename__ = "users"
    id = db.Column('user_id', db.Integer, primary_key=True)
    username = db.Column('username', db.String(20), unique=True , index=True)
    password_hash = db.Column('password_hash', db.String(1000))

    def __init__(self, username, password):
        self.username = username
        self.password = password

    @property
    def password(self):
        raise AttributeError('password is not a readable attribute/ password 不是一个可读属性。')

    @password.setter
    def password(self, password):
        self.password_hash = generate_password_hash(password, method='pbkdf2:sha256', salt_length=8)

    def verify_password(self, password):
        return check_password_hash(self.password_hash, password)

    @property
    def is_authenticated(self):
            return True

    @property
    def is_active(self):
        return True

    @property
    def is_anonymous(self):
        return False

    @property
    def get_id(self):
        return self.id


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


3个回答

变量名与函数名重复,导致调用函数时被认为是变量,所以不可调用。

kickfick123
kickfick123 是user吗?如果是的话,还是不行
大约 2 年之前 回复

不行啊,把login user中的(user)改成(u)还是有出现同样的问题图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问