这是我的用户身份验证方法:
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
身份验证后端开始拒绝不活动的用户姜戈 1.10 https://docs.djangoproject.com/en/1.10/releases/1.10/#django-contrib-auth。因此你的authenticate()
来电返回None
,你得到无效的用户名/密码!来自外部 if/else 语句的消息。
正如丹尼尔所说,如果你使用默认的ModelBackend
,您不再需要检查user.is_active
在您的登录视图中。
如果你真的想要authenticate
要返回非活动用户,那么您可以使用AllowAllUsersModelBackend https://docs.djangoproject.com/en/1.10/ref/contrib/auth/#django.contrib.auth.backends.AllowAllUsersModelBackend反而。如果您这样做,那么您有责任检查is_active
在您的登录视图中标记。
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)