gpt4 book ai didi

python - Django 登录表单/无效用户

转载 作者:行者123 更新时间:2023-11-30 22:53:11 24 4
gpt4 key购买 nike

我正在学习 Django 并开始构建一个简单的登录表单。当提供经过身份验证的用户名/密码时,我的 View 有效(重定向到“/person/”)。但在提供无效凭据时,它会抛出错误“AnonymousUser”对象没有属性“_meta”。以下是我的观点。

#views.py
def userlogin(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)
auth_login(request, user)
if user is not None:
if user.is_active:
print("User is valid, active and authenticated")
return HttpResponseRedirect('/person')
else:
print("The password is valid, but the account has been disabled!")
# return HttpResponseRedirect('/person/login')
return render(request, 'login.html')
else:
print("The username and password were incorrect.")
return HttpResponseRedirect('/person/login')

return render(request, 'login.html')

最佳答案

您正在尝试登录凭据无效的用户 ('AnonymousUser')。

auth_login 逻辑移至经过身份验证的用户的 block 中:

user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
auth_login(request, user)

关于python - Django 登录表单/无效用户,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38295686/

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