gpt4 book ai didi

Django - 用户 is_active

转载 作者:行者123 更新时间:2023-12-02 01:53:39 30 4
gpt4 key购买 nike

这是我的用户身份验证方法:

def user_login(request):
if request.method == 'POST':
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username, password=password)

if user:
if user.is_active:
login(request, user)
return HttpResponseRedirect(reverse('index'))
else:
print('TEST')
messages.info(request, 'Inactive user')
return HttpResponseRedirect(reverse('index'))
else:
messages.error(request, 'Invalid username/password!')
return HttpResponseRedirect(reverse('index'))
else:
return render(request, 'mainapp/login.html', {})

如果用户存在且不活动,则会出现错误消息:

messages.error(request, 'Invalid username/password!')
return HttpResponseRedirect(reverse('index'))

而不是:

print('TEST')
messages.info(request, 'Inactive user')
return HttpResponseRedirect(reverse('index'))

我不知道这里出了什么问题......有任何线索吗?

最佳答案

默认的 ModelBackend 身份验证后端开始拒绝 Django 1.10 中的非事件用户。因此,您的 authenticate() 调用返回 None,并且您会从外部 if/else 语句中收到 无效的用户名/密码! 消息。 p>

正如 Daniel 所说,如果您使用默认的 ModelBackend,则不再需要在登录 View 中检查 user.is_active

如果您确实希望身份验证返回非事件用户,那么您可以使用 AllowAllUsersModelBackend反而。如果您这样做,则您有责任检查登录 View 中的 is_active 标志。

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']

关于Django - 用户 is_active,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43184151/

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