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 是属性而不是方法,所以需要把后面的空格去掉

浙公网安备 33010602011771号