Skip to main content

AuthenticationForm

Base class for authenticating users. Extend this to get a form that accepts username/password logins.

Attributes

AttributeTypeDescription
username[UsernameField](usernamefield.md?sid=django_contrib_auth_forms_usernamefield)Form field used to collect the user's identification credential, configured with autofocus and dynamic length constraints based on the User model.
password[CharField](../../../forms/fields/charfield.md?sid=django_forms_fields_charfield)Form field used to collect the user's secret authentication credential, rendered as a password input to mask characters.
error_messagesdictA dictionary containing localized error messages for invalid login attempts and inactive account status.

Constructor

Signature

def AuthenticationForm(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) = None,
*args: tuple,
**kwargs: dict
) - > null

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) = NoneThe current HTTP request object, used for custom authentication logic.
*argstupleVariable length argument list passed to the parent form class.
**kwargsdictArbitrary keyword arguments, typically containing form data, passed to the parent form class.

Methods


clean()

@classmethod
def clean() - > dict

Validates the provided credentials by attempting to authenticate the user against the configured backend. If authentication fails or the user is not permitted to log in, it raises a ValidationError.

Returns

TypeDescription
dictThe dictionary of validated form data including the username and password.

confirm_login_allowed()

@classmethod
def confirm_login_allowed(
user: [User](../models/user.md?sid=django_contrib_auth_models_user)
) - > null

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.

Parameters

NameTypeDescription
user[User](../models/user.md?sid=django_contrib_auth_models_user)The user object instance to be checked for login eligibility.

Returns

TypeDescription
nullReturns None if the user is permitted to log in.

get_user()

@classmethod
def get_user() - > [User](../models/user.md?sid=django_contrib_auth_models_user)

Retrieves the authenticated user instance stored in the cache after successful validation.

Returns

TypeDescription
[User](../models/user.md?sid=django_contrib_auth_models_user)The authenticated user object, or None if authentication has not occurred or failed.

get_invalid_login_error()

@classmethod
def get_invalid_login_error() - > [ValidationError](../../../core/exceptions/validationerror.md?sid=django_core_exceptions_validationerror)

Constructs a ValidationError containing the specific error message and parameters for a failed login attempt.

Returns

TypeDescription
[ValidationError](../../../core/exceptions/validationerror.md?sid=django_core_exceptions_validationerror)A validation error object configured with the 'invalid_login' message and the localized username field name.