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

yipeiwu_com5年前Python基础

使用 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:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python抽象和自定义类定义与用法示例

本文实例讲述了Python抽象和自定义类定义与用法。分享给大家供大家参考,具体如下: 抽象方法 class Person(): def say(self): pass c...

在Python 中同一个类两个函数间变量的调用方法

在Python 中同一个类两个函数间变量的调用方法

如下所示: class A(): def test_a(self): self.m ="hello" def test_b(self): self.test...

python网络编程学习笔记(一)

学习用书:《python 网络编程基础》作者John Goerzen 第一部分底层网络学习         Pyth...

Python装饰器限制函数运行时间超时则退出执行

实际项目中会涉及到需要对有些函数的响应时间做一些限制,如果超时就退出函数的执行,停止等待。 可以利用python中的装饰器实现对函数执行时间的控制。 python装饰器简单来说可以在不改...

Python中在for循环中嵌套使用if和else语句的技巧

for...[if]...构建List (List comprehension) 1.简单的for...[if]...语句 Python中,for...[if]...语句一种简洁的构建L...