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

yipeiwu_com6年前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设计】。

相关文章

python3音乐播放器简单实现代码

本文实例为大家分享了python3音乐播放器的关键代码,供大家参考,具体内容如下 from tkinter import * from traceback import * from...

python将字符串转换成数组的方法

python将字符串转换成数组的方法。分享给大家供大家参考。具体实现方法如下: #----------------------------------------- # N...

python脚本设置超时机制系统时间的方法

python脚本设置超时机制系统时间的方法

本文为大家介绍了python脚本设置系统时间的方法,一共有两种,其一是调用socket直接发送udp包到国家授时中心,其二是调用ntplib包。我在本地电脑ping 国家授时中心地址cn...

Python元组操作实例分析【创建、赋值、更新、删除等】

Python元组操作实例分析【创建、赋值、更新、删除等】

本文实例讲述了Python元组操作。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 元组是跟列表非常相近的另一种容器类型。 元组是一种不可变类型,一旦创建不可...

Flask框架学习笔记之使用Flask实现表单开发详解

Flask框架学习笔记之使用Flask实现表单开发详解

本文实例讲述了使用Flask实现表单开发。分享给大家供大家参考,具体如下: <!DOCTYPE html> <html lang="en"> <head...