class AuthenticationForm
A form for logging a user in.
Takes request
as its first positional argument, which is stored on the form instance for use by sub-classes.
-
confirm_login_allowed(user)
-
By default,
AuthenticationForm
rejects users whoseis_active
flag is set toFalse
. You may override this behavior with a custom policy to determine which users can log in. Do this with a custom form that subclassesAuthenticationForm
and overrides theconfirm_login_allowed()
method. This method should raise aValidationError
if the given user may not log in.For example, to allow all users to log in regardless of “active” status:
12345from
django.contrib.auth.forms
import
AuthenticationForm
class
AuthenticationFormWithInactiveUsersOkay(AuthenticationForm):
def
confirm_login_allowed(
self
, user):
pass
(In this case, you’ll also need to use an authentication backend that allows inactive users, such as as
AllowAllUsersModelBackend
.)Or to allow only some active users to log in:
123456789101112class
PickyAuthenticationForm(AuthenticationForm):
def
confirm_login_allowed(
self
, user):
if
not
user.is_active:
raise
forms.ValidationError(
_(
"This account is inactive."
),
code
=
'inactive'
,
)
if
user.username.startswith(
'b'
):
raise
forms.ValidationError(
_(
"Sorry, accounts starting with 'b' aren't welcome here."
),
code
=
'no_b_users'
,
)
Please login to continue.