Skip to main content

LoginView

Display the login form and handle the login action.

Attributes

AttributeTypeDescription
form_classclass = AuthenticationFormThe form class used to handle the login logic, defaulting to AuthenticationForm.
authentication_formclass = nullAn optional form class that, if provided, overrides the default form_class for user authentication.
template_namestring = "registration/login.html"The path to the template used to render the login page.
redirect_authenticated_userboolean = falseA boolean flag that, when true, redirects already authenticated users away from the login page to prevent redundant logins.
extra_contextdict = nullA 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

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request object used to check user authentication status
argstuplePositional arguments passed to the view
kwargsdictKeyword arguments passed to the view

Returns

TypeDescription
[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

TypeDescription
strThe 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

TypeDescription
typeThe 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

TypeDescription
dictA 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

NameTypeDescription
form[Form](../../../forms/forms/form.md?sid=django_forms_forms_form)The validated authentication form instance containing the authenticated user object

Returns

TypeDescription
[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

NameTypeDescription
kwargsdictAdditional keyword arguments to be merged into the template context

Returns

TypeDescription
dictThe context dictionary containing site details and redirection logic for template rendering