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

相关文章

Python实现正则表达式匹配任意的邮箱方法

Python实现正则表达式匹配任意的邮箱方法

首先来个简单的例子,利用Python实现匹配163邮箱的代码: #-*- coding:utf-8 -*- __author__ = '杨鑫' import re text = in...

利用numpy+matplotlib绘图的基本操作教程

利用numpy+matplotlib绘图的基本操作教程

简述 Matplotlib是一个基于python的2D画图库,能够用python脚本方便的画出折线图,直方图,功率谱图,散点图等常用图表,而且语法简单。具体介绍见matplot官网。...

python pyheatmap包绘制热力图

利用python pyheatmap包绘制热力图,供大家参考,具体内容如下 import matplotlib.pyplot as plt from pyheatmap.heatma...

面向初学者的Python编辑器Mu

面向初学者的Python编辑器Mu

Meet Mu,一个开放源码编辑器,使学生们更容易学习编写Python代码。 Mu一个开源编辑器,是满足学生可以轻松学习编写Python代码的工具。作为初学程序员的Python编辑器,旨...

Python实现的下载网页源码功能示例

Python实现的下载网页源码功能示例

本文实例讲述了Python实现的下载网页源码功能。分享给大家供大家参考,具体如下: #!/usr/bin/python import httplib httpconn = httpl...