gpt4 book ai didi

python - 如何判断用户的状态?

转载 作者:太空宇宙 更新时间:2023-11-04 10:30:45 26 4
gpt4 key购买 nike

请帮忙解决问题。

是定义用户登录与否的函数: View .py:

def ajax_is_authenticated_check(request):
"""
ajax check auth for like process and any
"""
result = False

if request.method == 'POST' and request.is_ajax():
if request.user.is_authenticated:
result = True

data = {
'is_authenticated': result
}

print(result) #this is output to console

return HttpResponse(json.dumps(data), content_type='application/json')

结果函数总是返回“真”。并输出控制台“真”。

不清楚为什么,如果用户没有输入,它仍然返回“true”

最佳答案

那是因为 is_authenticated 是一个方法,而不是一个属性。

代码应该是这样的

if request.method == 'POST' and request.is_ajax():
if request.user.is_authenticated(): # note the ()
result = True

关于python - 如何判断用户的状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26815620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com