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.

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

 

相关文章
|
PyTorch 算法框架/工具
Pytorch出现‘Tensor‘ object is not callable解决办法
Pytorch出现‘Tensor‘ object is not callable解决办法
498 0
Pytorch出现‘Tensor‘ object is not callable解决办法
Python错误 TypeError: ‘NoneType‘ object is not subscriptable解决方案汇总
Python错误 TypeError: ‘NoneType‘ object is not subscriptable解决方案汇总
|
7天前
|
存储 JSON JavaScript
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
【Python】已完美解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
10 1
|
7天前
|
JSON 前端开发 数据格式
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
【Python】已解决:TypeError: Object of type JpegImageFile is not JSON serializable
10 0
|
7天前
|
开发者 Python
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
【Python】已解决:TypeError: descriptor ‘index‘ for ‘list‘ objects doesn‘t apply to a ‘str‘ object
9 0
|
7天前
|
开发者 Python
【Python】已解决:TypeError: a bytes-like object is required, not ‘int’
【Python】已解决:TypeError: a bytes-like object is required, not ‘int’
9 0
|
2月前
|
JSON 数据格式
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
解决报错TypeError: Converting circular structure to JSON --> starting at object with constructor
|
2月前
|
JSON 数据格式 Python
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
TypeError the JSON object must be str, bytes or bytearray, not ‘list‘
129 1
|
2月前
|
Python
完美解决丨+# TypeError: ‘dict_keys‘ object does not support indexing
完美解决丨+# TypeError: ‘dict_keys‘ object does not support indexing
|
8月前
|
机器学习/深度学习 自然语言处理 数据可视化
简单的知识图谱可视化+绘制nx.Graph()时报错TypeError: ‘_AxesStack‘ object is not callable
简单的知识图谱可视化+绘制nx.Graph()时报错TypeError: ‘_AxesStack‘ object is not callable
156 0