在我的应用程序中,我有一个使用django-custom-user的自定义用户模型,也正在使用allauth。 Django-axes在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.backends.AxesModelBackend
添加到AUTHENTICATION_BACKENDS
时才出现此错误。
答案 0 :(得分:0)
对我来说
form = AuthenticationForm(data=request.POST, request=request)
user = authenticate(username=username, password=password, request=request)
并进一步使用装饰器进行“登录”视图
from axes.decorators import axes_dispatch
@axes_dispatch
是解决方案。也许对您也有帮助!
最好的问候