跟着欢迎进入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:
然后就不报错了。

解决Flask和Django的错误“TypeError: 'bool' object is not callable”的更多相关文章

  1. python import 错误 TypeError: 'module' object is not callable

    python import 错误 TypeError: 'module' object is not callable 在这里,有 Person.py test.py; 在 test.py 里面 im ...

  2. flask渲染模板时报错TypeError: 'UnboundField' object is not callable

    渲染模板时,访问页面提示TypeError: 'UnboundField' object is not callable 检查代码,发现实例化表单类是,没有加括号:form = NewNoteForm ...

  3. java/javac命令行如何同时引用多个包;错误 TypeError: 'JavaPackage' object is not callable 的含义

    出现这类错误提示:'JavaPackage' object is not callable,可以看下所引用的jar包或者class文件是否在java的路径搜索范围内 命令行模式下:javac可以编译* ...

  4. TypeError: 'bool' object is not callable g.user.is_authenticated()

    此问题查了stackoverflow后知道is_authenticated是一个属性而不是一个方法所以g.user.is_authenticated() 用法会报错

  5. Django 错误:TypeError at / 'bool' object is not callable

    使用 Django自带的 auth 用户验证功能,编写函数,使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object is not ...

  6. python Flask :TypeError: 'dict' object is not callable

    flask 基于Werkzeug .. @moudule.route('/upload', methods=['GET', 'POST']) def upload_file(): global _fl ...

  7. 解决:TypeError: 'list' object is not callable

    如果list变量和list函数重名,会有什么后果呢?我们可以参考如下代码: list = ['泡芙', '汤圆', '鱼儿', '骆驼'] tup_1 = (1, 2, 3, 4, 5) tupToL ...

  8. appium 提示报错“TypeError: 'unicode' object is not callable”的解决方式!

    这里提到的这个报错,是小错误且容易经常会犯,有时需要特别注意使用. 目的要求结果:根据某个元素的id值获取到对应id的text值,并且将获取的text值与本身存在的text值做比较,查看text值是否 ...

  9. 调用日志输出错误:TypeError: 'int' object is not callable等

    *)TypeError: 'int' object is not callable 错误信息: Traceback (most recent call last): File "Visual ...

随机推荐

  1. 《Hands-On System Programming with Go》之目录操作

    开一个新书<Hands-On System Programming with Go>,系统的了解一下, 这方面的东东,以前用C语言实现过, 现在用GO,重新来!! package main ...

  2. Python 的 Geth 封装库 PyGeth

    PyGeth 是一个 Python 封装库,用来作为子进程运行 geth. 系统依赖 该库需要 geth 可执行文件. 安装 pip install py-geth 快速启动 运行连接到 mainne ...

  3. opencv检测图像直线

    #include<opencv2/opencv.hpp> #include<iostream> using namespace std; using namespace cv; ...

  4. C语言笔记 06_作用域&数组

    作用域 任何一种编程中,作用域是程序中定义的变量所存在的区域,超过该区域变量就不能被访问.C 语言中有三个地方可以声明变量: 在函数或块内部的局部变量 在所有函数外部的全局变量 在形式参数的函数参数定 ...

  5. Spring Boot 静态资源能加载css 不能加载js

    Spring Boot 配置拦截器的时候默认 是放行 静态资源 , 也就是说不需要进行配置 registry.addResourceHandler("/**") .addResou ...

  6. Ligg.WinOa-000: Windows运维自动化编程实战--前言

        本开源项目Ligg.WinOa是一个基于Ligg.EasyWinApp的Windows运维自动化应用.通过Ligg.EasyWinForm生成2个功能界面:管理员工具箱和用户工具箱:通过Lig ...

  7. ES6-Proxy,代理

    proxy 代理 Es6 增强 对象和函数(方法)   Proxy用于修改某些操作的默认行为,即对编程语言层面进行修改,属于“元编程”, Proxy意思为“代理”,即在访问对象之前建立一道“拦截”,任 ...

  8. 前端开发规范:4-JS

    ESLint 使用ESLint的standard规范来编写js代码 更多参考: https://github.com/standard/standard/blob/master/docs/README ...

  9. 通过程序调用微信公众号发消息api返回48001

    自己的订阅号,尝试通过写程序来给用户发消息.结果呢,接口返回报错:errcode=48001,errmsg = api unauthorized hint: [ZlPULa02942276!] 去微信 ...

  10. 工具-Xmind常用快捷键/使用

    1-快捷键 Ctrl+Shift+L 快捷键助手 Ctrl+Home 返回中心主题 Ctrl+] 插入摘要 Ctrl+I 插入图片 Ctrl+Shift+H 插入超链接 Ctrl+1,2,3,4,5, ...