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 判断 有向图 是否有环的实例讲解

实例如下: import numpy from numpy import * def dfs( v ): vis[v] = -1 flag = 0 for i in range...

python绘图模块matplotlib示例详解

python绘图模块matplotlib示例详解

前言 Matplotlib 是 Python 的绘图库。作为程序员,经常需要进行绘图,在我自己的工作中,如果需要绘图,一般都是将数据导入到excel中,然后通过excel生成图表,这样操...

Django基础三之视图函数的使用方法

Django基础三之视图函数的使用方法

一 Django的视图函数view 一个视图函数(类),简称视图,是一个简单的Python 函数(类),它接受Web请求并且返回Web响应。 响应可以是一张网页的HTML内容,一个重定向...

Python编写Windows Service服务程序

Python编写Windows Service服务程序

 如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模...

把MySQL表结构映射为Python中的对象的教程

ORM mysql的表结构是二维表,用python的数据结构表示出来就是一个列表,每一个记录是一个tuple。如下所示: [('1', ''huangyi),('2', ''letian...