LoginView
Display the login form and handle the login action.
Attributes
| Attribute | Type | Description |
|---|---|---|
| form_class | class = AuthenticationForm | The form class used to handle the login logic, defaulting to AuthenticationForm. |
| authentication_form | class = null | An optional form class that, if provided, overrides the default form_class for user authentication. |
| template_name | string = "registration/login.html" | The path to the template used to render the login page. |
| redirect_authenticated_user | boolean = false | A boolean flag that, when true, redirects already authenticated users away from the login page to prevent redundant logins. |
| extra_context | dict = null | A dictionary of additional data to be included in the template context during rendering. |
Methods
dispatch()
@classmethod
def dispatch(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
args: tuple,
kwargs: dict
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Handles the initial request and redirects authenticated users to the success URL if redirect_authenticated_user is enabled, preventing redirection loops.
Parameters
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming HTTP request object used to check user authentication status |
| args | tuple | Positional arguments passed to the view |
| kwargs | dict | Keyword arguments passed to the view |
Returns
| Type | Description |
|---|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | An HTTP response or redirect depending on the user's authentication status and view configuration |
get_default_redirect_url()
@classmethod
def get_default_redirect_url() - > str
Return the default redirect URL.
Returns
| Type | Description |
|---|---|
str | The resolved URL string from the next_page attribute or the LOGIN_REDIRECT_URL setting |
get_form_class()
@classmethod
def get_form_class() - > type
Retrieves the form class to be used for authentication, prioritizing the authentication_form attribute over the default form_class.
Returns
| Type | Description |
|---|---|
type | The Django form class used to render and validate the login credentials |
get_form_kwargs()
@classmethod
def get_form_kwargs() - > dict
Builds the keyword arguments for the form instance, injecting the current request object into the form's initialization data.
Returns
| Type | Description |
|---|---|
dict | A dictionary of arguments including the current request, used to instantiate the authentication form |
form_valid()
@classmethod
def form_valid(
form: [Form](../../../forms/forms/form.md?sid=django_forms_forms_form)
) - > [HttpResponseRedirect](../../../http/response/httpresponseredirect.md?sid=django_http_response_httpresponseredirect)
Security check complete. Log the user in.
Parameters
| Name | Type | Description |
|---|---|---|
| form | [Form](../../../forms/forms/form.md?sid=django_forms_forms_form) | The validated authentication form instance containing the authenticated user object |
Returns
| Type | Description |
|---|---|
[HttpResponseRedirect](../../../http/response/httpresponseredirect.md?sid=django_http_response_httpresponseredirect) | A redirect response to the success URL after the user session is established |
get_context_data()
@classmethod
def get_context_data(
kwargs: dict
) - > dict
Populates the template context with the redirect URL, current site information, and any additional extra_context provided.
Parameters
| Name | Type | Description |
|---|---|---|
| kwargs | dict | Additional keyword arguments to be merged into the template context |
Returns
| Type | Description |
|---|---|
dict | The context dictionary containing site details and redirection logic for template rendering |