AuthenticationForm
Base class for authenticating users. Extend this to get a form that accepts username/password logins.
Attributes
| Attribute | Type | Description |
|---|---|---|
| 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_messages | dict | A 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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) = None | The current HTTP request object, used for custom authentication logic. |
| *args | tuple | Variable length argument list passed to the parent form class. |
| **kwargs | dict | Arbitrary 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
| Type | Description |
|---|---|
dict | The 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
| Name | Type | Description |
|---|---|---|
| user | [User](../models/user.md?sid=django_contrib_auth_models_user) | The user object instance to be checked for login eligibility. |
Returns
| Type | Description |
|---|---|
null | Returns 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
| Type | Description |
|---|---|
[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
| Type | Description |
|---|---|
[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. |