其他分享
首页 > 其他分享> > Django项目 使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object is not callable 

Django项目 使用 is_authenticated 检查用户是否登录,结果报错: TypeError at / 'bool' object is not callable 

作者:互联网

代码如下:

def login(request):
if request.method == "POST":
username = request.POST.get("username")
pwd = request.POST.get("password")
# 如何判断用户名和密码对不对
user = auth.authenticate(username=username, password=pwd)
if user:
ret = user.is_authenticated() #这里是提示报错的地方
print(ret)
print("-" * 120)

# 将登录的用户封装到request.user
auth.login(request, user)
return redirect("/index/")
return render(request, "login.html")

解决方法:
ret = user.is_authenticated    #    is_authenticated 是属性而不是方法,所以需要把后面的空格去掉

标签:username,TypeError,authenticated,request,ret,报错,user,POST
来源: https://www.cnblogs.com/zs0621/p/16122883.html