Flask-Login raises TypeError: 'int' object is not callable
Flask-Login raises TypeError: 'int' object is not callable
我只是写了一个flask登录demo
@app.route('/reg/', methods=['GET', 'POST'])
def reg():
username = request.form.get('username').strip()
password = request.form.get('password').strip()
if (username == '' or password == ''):
return redirect_with_msg('/regloginpage/', u'用户名或密码不能为空', category='reglogin')
user = User.query.filter_by(username=username).first()
if (user != None):
return redirect_with_msg('/regloginpage/', u'用户名已存在', category='reglogin')
salt = '.'.join(random.sample('0123456789abcdfeghijklmnABCDEFG', 10))
m = hashlib.md5()
str1 = (password + salt).encode('utf-8')
m.update(str1)
password = m.hexdigest()
user = User(username, password, salt)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect('/')
像这样追溯:
TypeError: 'int' object is not callable
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/apple/PycharmProjects/pinstagram/pinstagram/views.py", line 94, in login
login_user(user)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_login/utils.py", line 140, in login_user
user_id = getattr(user, current_app.login_manager.id_attribute)()
TypeError: 'int' object is not callable
这让我很难过,有人能救救我吗?
阅读错误信息发现错误发生在utils.py的第140行。这可能是因为您有
user_id = getattr(user, current_app.login_manager.id_attribute)()
最后的 () 使您的程序尝试将 getattr 的 return 值作为函数调用,当它是一个 int 时。删除 () 它应该可以工作。
我刚刚 运行 遇到了与 flask-login 相同的问题并解决了这个问题。虽然 @Sweater-Baron 的回答暗示了这个问题,但这是 User
class 中的直接修复,因为编辑 Flask-Login
没有意义。方法 get_id()
不应声明为 属性:
@property
def get_id(self):
return self.uid
不同于 is_authenticated()
、is_active()
和 is_anonymous()
。
def get_id(self):
return self.uid
来自 Flask-Login 文档:
get_id()
This method must return a unicode that uniquely identifies this user, and
can be used to load the user from the user_loader callback.
[重点是我的]
我只是写了一个flask登录demo
@app.route('/reg/', methods=['GET', 'POST'])
def reg():
username = request.form.get('username').strip()
password = request.form.get('password').strip()
if (username == '' or password == ''):
return redirect_with_msg('/regloginpage/', u'用户名或密码不能为空', category='reglogin')
user = User.query.filter_by(username=username).first()
if (user != None):
return redirect_with_msg('/regloginpage/', u'用户名已存在', category='reglogin')
salt = '.'.join(random.sample('0123456789abcdfeghijklmnABCDEFG', 10))
m = hashlib.md5()
str1 = (password + salt).encode('utf-8')
m.update(str1)
password = m.hexdigest()
user = User(username, password, salt)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect('/')
像这样追溯:
TypeError: 'int' object is not callable
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/Users/apple/PycharmProjects/pinstagram/pinstagram/views.py", line 94, in login
login_user(user)
File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/flask_login/utils.py", line 140, in login_user
user_id = getattr(user, current_app.login_manager.id_attribute)()
TypeError: 'int' object is not callable
这让我很难过,有人能救救我吗?
阅读错误信息发现错误发生在utils.py的第140行。这可能是因为您有
user_id = getattr(user, current_app.login_manager.id_attribute)()
最后的 () 使您的程序尝试将 getattr 的 return 值作为函数调用,当它是一个 int 时。删除 () 它应该可以工作。
我刚刚 运行 遇到了与 flask-login 相同的问题并解决了这个问题。虽然 @Sweater-Baron 的回答暗示了这个问题,但这是 User
class 中的直接修复,因为编辑 Flask-Login
没有意义。方法 get_id()
不应声明为 属性:
@property
def get_id(self):
return self.uid
不同于 is_authenticated()
、is_active()
和 is_anonymous()
。
def get_id(self):
return self.uid
来自 Flask-Login 文档:
get_id()
This method must return a unicode that uniquely identifies this user, and can be used to load the user from the user_loader callback.
[重点是我的]