解决Flask错误“TypeError: 'bool' object is not callable”
程序员文章站
2022-06-22 10:54:39
跟着欢迎进入Flask大型教程项目!的教程学习Flask,到了重构用户模型的时候,运行脚本后报错: TypeError: 'bool' object is not callable 这是用户模型: class User(db.Model): id = db.Column(db.Integer, pr ......
跟着欢迎进入flask大型教程项目!的教程学习flask,到了重构用户模型的时候,运行脚本后报错:
typeerror: 'bool' object is not callable
这是用户模型:
class user(db.model): id = db.column(db.integer, primary_key=true) nickname = db.column(db.string(64), index=true, unique=true) email = db.column(db.string(120), index=true, unique=true) posts = db.relationship('post', backref='author', lazy='dynamic') @property def is_authenticated(self): return true @property def is_active(self): return true @property def is_anonymous(self): return false def get_id(self): try: return unicode(self.id) # python 2 except nameerror: return str(self.id) # python 3 def __repr__(self): return '<user %r>' % (self.nickname)
这是调用的时候的代码:
from flask import render_template, flash, redirect, session, url_for, request, g from flask.ext.login import login_user, logout_user, current_user, login_required from app import app, db, lm, oid from .forms import loginform from .models import user @app.route('/login', methods=['get', 'post']) @oid.loginhandler def login(): if g.user is not none and g.user.is_authenticated(): # 这一句报错 return redirect(url_for('index')) form = loginform() if form.validate_on_submit(): session['remember_me'] = form.remember_me.data return oid.try_login(form.openid.data, ask_for=['nickname', 'email']) return render_template('login.html', title='sign in', form=form, providers=app.config['openid_providers'])
解决方法:
按照参考资料里面的说法:
is_authenticated是属性而不是方法,把括号去掉就可以了。书里这一段有两处印刷错误,请参照git源码。
把出错的地方:if g.user is not none and g.user.is_authenticated():
修改为if g.user is not none and g.user.is_authenticated:
然后就不报错了。
转自:https://www.cnblogs.com/zmdComeOn/p/11452652.html
下一篇: 爬虫计算两张验证图片的距离(情况一)
推荐阅读
-
小白入门常见错误python:TypeError: ‘generator‘ object is not subscriptable 的解决方法(萌新踩雷!!!)
-
解决Flask错误“TypeError: 'bool' object is not callable”
-
【文件处理】TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法
-
成功解决TypeError: ‘tuple‘ object is not callable
-
Django关于TypeError: 'tuple' object is not callable解决方式
-
frcnn系列错误: TypeError: 'numpy.float64' object cannot be interpreted as an index 的解决方案
-
错误TypeError: object of type <class ‘numpy.float64‘> cannot be safely interpreted as an integer.解决方案
-
解决Flask错误“TypeError: 'bool' object is not callable”
-
解决TypeError: 'NoneType' object is not callable
-
【文件处理】TypeError: Object of type ‘ndarray‘ is not JSON serializable错误解决方法