在我的应用程序中,我有一个使用django自定义用户的自定义用户模型,我也使用allauth。Django axis在admin和allauth上注册每一次登录尝试,但我对用户登录有问题。
登录视图:
def user_login(request):
data = dict()
if request.user.is_authenticated:
return redirect('frontend:dashboard')
if request.method == "POST":
form = LoginForm(request.POST)
if form.is_valid():
email = form.cleaned_data['email']
password = form.cleaned_data['password']
if user is not None:
user = authenticate(email=email, password=password)
if user.is_active:
login(request, user)
user_logged_in.send(sender=CustomUser, request=request, user=user)
return redirect('frontend:dashboard')
else:
messages.error(request, _('Incorrect email or password.'))
user_login_failed.send(sender=CustomUser, request=request, credentials={'username': form.cleaned_data.get('email')})
else:
messages.error(request, _('Incorrect email or password.'))
user_login_failed.send(sender=CustomUser, request=request, credentials={'username': form.cleaned_data.get('email')})
return redirect('auth:login')
else:
form = LoginForm()
data['form'] = form
return render(request, os.path.join(settings.AUTH_TEMPLATE, 'login.html'), data)
当我尝试登录时,我得到了这个错误:
axes.backends.AxesModelBackend.RequestParameterRequired: DjangoAxesModelBackend requires calls to authenticate to pass `request`
django-axes文档有点令人困惑,我不明白解决这个问题需要做什么。
我必须指出,只有当我将axes.backends.AxesModelBackend
添加到AUTHENTICATION_BACKENDS
时,才会出现此错误,这是强制性的。
对我来说:
form = AuthenticationForm(data=request.POST, request=request)
user = authenticate(username=username, password=password, request=request)
并且进一步使用登录视图的装饰器
from axes.decorators import axes_dispatch
@axes_dispatch
是解决方案。也许它也会帮助你!
向致以最良好的问候