gpt4 book ai didi

django - 使用自定义 AuthenticationForm 覆盖 Django LoginView 错误消息

转载 作者:行者123 更新时间:2023-12-04 02:36:43 30 4
gpt4 key购买 nike

我有一个基于类的 View ,子类 LoginView .

from django.contrib.auth.views import LoginView

class CustomLoginView(LoginView):

def get_success_url(self):
url = self.get_redirect_url()
return url or reverse_lazy('knowledgebase:user_home', kwargs={
'username':self.request.user.username,
})

如果用户的电子邮件尚未激活,我想覆盖错误消息,因为他们必须单击发送到其电子邮件地址的链接。当前的默认消息如下所示:

override LoginView error message

而不是说:

Please enter a correct email address and password. Note that both fields may be case-sensitive.



我想说几句大意:

Please confirm your email so you can log in.



我试过:

帐户/forms.py
from django.contrib.auth.forms import AuthenticationForm
from django.utils.translation import gettext as _

class PickyAuthenticationForm(AuthenticationForm):
def confirm_login_allowed(self, user):
if not user.is_active:
raise forms.ValidationError(
_("Please confirm your email so you can log in."),
code='inactive',
)

帐户/views.py
class CustomLoginView(LoginView): # 1. <--- note: this is a class-based view

form_class = PickyAuthenticationForm # 2. <--- note: define form here?

def get_success_url(self):
url = self.get_redirect_url()
return url or reverse_lazy('knowledgebase:user_home', kwargs={
'username':self.request.user.username,
})

当我尝试使用 的用户登录时,结果绝对没有效果。 存在,但尚未验证他们的电子邮件地址。

AuthenticationForm docs .

最佳答案

方法 - 1
Django 使用 ModelBackend 默认 AUTHENTICATION_BACKENDS 并且不验证非事件用户。
Authorization for inactive users 中也说明了这一点。部分,

An inactive user is one that has its is_active field set to False. TheModelBackend and RemoteUserBackend authentication backends prohibitsthese users from authenticating. If a custom user model doesn’t havean is_active field, all users will be allowed to authenticate.


所以,设置 AllowAllUsersModelBackend 作为您的 AUTHENTICATION_BACKENDS settings.py
# settings.py

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

它对我的 Django 应用程序有多大影响?
它不会影响身份验证以外的任何内容。如果我们查看 source code of AllowAllUsersModelBackend class我们可以看到它只允许 非事件 用户进行身份验证。

方法 - 2
就个人而言,我不推荐这种方法,因为方法 1 是 Django 解决这个问题的方法。
覆盖 clean(...) 的方法| PickyAuthenticationForm 上课并调用 AllowAllUsersModelBackend 后端为,
from django.contrib.auth.backends import AllowAllUsersModelBackend


class PickyAuthenticationForm(AuthenticationForm):
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')

if username is not None and password:
backend = AllowAllUsersModelBackend()
self.user_cache = backend.authenticate(self.request, username=username, password=password)
if self.user_cache is None:
raise self.get_invalid_login_error()
else:
self.confirm_login_allowed(self.user_cache)

return self.cleaned_data


def confirm_login_allowed(self, user):
if not user.is_active:
raise forms.ValidationError(
"Please confirm your email so you can log in.",
code='inactive',
)
结果截图
Screenshot

关于django - 使用自定义 AuthenticationForm 覆盖 Django LoginView 错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61470820/

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