Django错误:TypeError at / 'bool' object is not callable解决

生命不是要超越别人,而是要超越自己。每天醒来并告诉自己:更少的理由,更大的腹部,更甜的嘴,更小的脾气,更快的动作,更高的效率,一点微笑和脑。一站式

使用 Django自带的 auth 用户验证功能,编写函数,使用 is_authenticated 检查用户是否登录,结果报错:

TypeError at / 'bool' object is not callable  

编写函数如下:

def index(request, pid=None, del_pass=None):
  if request.user.is_authenticated():
    username = request.user.username
    useremail = request.user.email
  messages.get_messages(request)
  template = get_template('index.html')
  html = template.render(context=locals(), request=request)
  return HttpResponse(html)

查询相关资料,发现 is_authenticated 是属性而不是方法,我们应该把括号去掉,这样就没什么问题了。

将 

if request.user.is_authenticated():

改为

if request.user.is_authenticated:

本文Django错误:TypeError at / 'bool' object is not callable解决到此结束。懂得感恩,是收获幸福的源泉。懂得感恩,你会发现原来自我周围的一切都是那样的完美。小编再次感谢大家对我们的支持!

您可能有感兴趣的文章
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str

django连接数据库获取数据的简单步骤记录

Django实现翻页的示例代码

正确的理解和使用Django信号(Signals)

django使用多个数据库的方法实例