如何通知用户他在Django登录视图中未处于活动状态 [英] How to inform a user that he is not active in Django login view
问题描述
我正在为Django创建一个自定义身份验证应用,一切正常,用户需要验证电子邮件以将is_active设置为True,这就像一个超级按钮,但是一旦用户发送电子邮件且令牌过期,用户尝试登录并接收到用户消息或密码传递不正确,我想表明用户必须激活该帐户并提供一个链接,以将激活令牌重新发送到他的电子邮件.
I'm creating a custom authentication app to Django, everything is fine, the user needs to verify email to set is_active True, this works like a charm, but once the user send the e-mail and the token expires, the user try to login and receive a message of user or pass incorrect, and I want to show the user has to activate the account and give a link to resend the activation token to his email.
我正在使用默认的登录视图:
I'm using the default Login View:
url(r'^login/$', auth_views.LoginView.as_view(template_name='accounts/login.html'), name='account_login')
我如何修改此视图或其他内容以显示用户未处于活动状态?
How can i modify this view or other thing to show the user is not active?
从Django auth中查看AuthenticationForm,我可以看到存在一个非活动用户错误,但是只有错误inin_login出现在登录表单中.
Looking in the AuthenticationForm from Django auth I can see that there is an error of inactive user, but only the error invalid_login is being raised in login form.
class AuthenticationForm(forms.Form):
"""
Base class for authenticating users. Extend this to get a form that accepts
username/password logins.
"""
username = UsernameField(
max_length=254,
widget=forms.TextInput(attrs={'autofocus': True}),
)
password = forms.CharField(
label=_("Password"),
strip=False,
widget=forms.PasswordInput,
)
error_messages = {
'invalid_login': _(
"Please enter a correct %(username)s and password. Note that both "
"fields may be case-sensitive."
),
'inactive': _("This account is inactive."),
}
def __init__(self, request=None, *args, **kwargs):
"""
The 'request' parameter is set for custom auth use by subclasses.
The form data comes in via the standard 'data' kwarg.
"""
self.request = request
self.user_cache = None
super(AuthenticationForm, self).__init__(*args, **kwargs)
# Set the label for the "username" field.
self.username_field = UserModel._meta.get_field(UserModel.USERNAME_FIELD)
if self.fields['username'].label is None:
self.fields['username'].label = capfirst(self.username_field.verbose_name)
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
if username is not None and password:
self.user_cache = authenticate(self.request, username=username, password=password)
if self.user_cache is None:
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
params={'username': self.username_field.verbose_name},
)
else:
self.confirm_login_allowed(self.user_cache)
return self.cleaned_data
def confirm_login_allowed(self, user):
"""
Controls whether the given User may log in. This is a policy setting,
independent of end-user authentication. This default behavior is to
allow login by active users, and reject login by inactive users.
If the given user cannot log in, this method should raise a
``forms.ValidationError``.
If the given user may log in, this method should return None.
"""
if not user.is_active:
raise forms.ValidationError(
self.error_messages['inactive'],
code='inactive',
)
def get_user_id(self):
if self.user_cache:
return self.user_cache.id
return None
def get_user(self):
return self.user_cache
推荐答案
我能够从AuthenticationForm类中纠正这种覆盖过度的干净方法.
I'm able to correct this overrinding clean method from AuthenticationForm class.
class LoginForm(AuthenticationForm):
def clean(self):
username = self.cleaned_data.get('username')
password = self.cleaned_data.get('password')
if username is not None and password:
self.user_cache = authenticate(self.request, username=username, password=password)
if self.user_cache is None:
try:
user_temp = User.objects.get(username=username)
except:
user_temp = None
if user_temp is not None:
self.confirm_login_allowed(user_temp)
else:
raise forms.ValidationError(
self.error_messages['invalid_login'],
code='invalid_login',
params={'username': self.username_field.verbose_name},
)
return self.cleaned_data
永远不会引发不活动用户,这是因为在Django 1.10之后,所有不活动的用户都无法进行身份验证,因此self.user_chache对于不活动的用户始终为None,即使拥有正确的用户并通过.
The inactive user never is raised, this happens because after Django 1.10 all users that is not active cannot authenticate, so self.user_chache is always be None for inactive users, even if has a correct user and pass.
这篇关于如何通知用户他在Django登录视图中未处于活动状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!