Python 如何确定用户的状态?
请帮忙解决这个问题 定义用户是否登录的函数: views.py:Python 如何确定用户的状态?,python,django,Python,Django,请帮忙解决这个问题 定义用户是否登录的函数: views.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_authenticat
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”。和输出控制台“true”
不清楚为什么,如果用户没有输入,它仍然返回“true”,这是因为
经过身份验证
是一种方法,而不是属性
代码应该是这样的
if request.method == 'POST' and request.is_ajax():
if request.user.is_authenticated(): # note the ()
result = True
这是因为
是经过身份验证的
是一种方法,而不是属性
代码应该是这样的
if request.method == 'POST' and request.is_ajax():
if request.user.is_authenticated(): # note the ()
result = True
从Django 1.10开始,
是否经过身份验证
是一个属性,您可以使用请求.user.is\u身份验证
对于低于1.10的版本,您需要在视图中使用request.user.is\u authenticated()
注意:在所有版本的模板中,您可以使用从Django 1.10开始的
request.user.is_authenticated
is_authenticated
是一个属性,您可以使用request.user.is_authenticated
对于低于1.10的版本,您需要在视图中使用request.user.is\u authenticated()
注意:在所有版本的模板中,您都可以使用request.user.is\u authenticated
的可能副本