Skip to main content

LoginRequiredMiddleware

Middleware that redirects all unauthenticated requests to a login page.

Attributes

AttributeTypeDescription
redirect_field_namestring = REDIRECT_FIELD_NAMEThe name of the query string parameter that will contain the URL the user should be redirected to after a successful login.

Methods


process_view()

@classmethod
def process_view(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
view_func: callable,
view_args: tuple,
view_kwargs: dict
) - > HttpResponse|None

Evaluates the authentication status of the request before the view is executed. It returns None if the view is exempt or the user is authenticated, otherwise it triggers the redirection logic.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current web request object being processed.
view_funccallableThe Python function or class-based view that Django is about to execute.
view_argstuplePositional arguments that will be passed to the view function.
view_kwargsdictKeyword arguments that will be passed to the view function.

Returns

TypeDescription
`HttpResponseNone`

get_login_url()

@classmethod
def get_login_url(
view_func: callable
) - > string

Determines the URL where unauthenticated users should be redirected. It prioritizes URLs defined on the view itself before falling back to the global project settings.

Parameters

NameTypeDescription
view_funccallableThe view function being accessed, used to check for a view-specific login URL override.

Returns

TypeDescription
stringThe absolute or relative URL string of the login page.

get_redirect_field_name()

@classmethod
def get_redirect_field_name(
view_func: callable
) - > string

Retrieves the name of the query parameter used to store the destination URL after a successful login. This allows the login view to redirect the user back to their original destination.

Parameters

NameTypeDescription
view_funccallableThe view function being accessed, used to check for a view-specific redirect field name override.

Returns

TypeDescription
stringThe name of the GET parameter, such as 'next'.

handle_no_permission()

@classmethod
def handle_no_permission(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest),
view_func: callable
) - > [HttpResponseRedirect](../../../http/response/httpresponseredirect.md?sid=django_http_response_httpresponseredirect)

Processes the redirection for unauthenticated users by calculating the return path and generating a redirect response. It ensures the 'next' parameter is correctly formatted based on the login URL's domain.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The current web request object used to determine the original destination path.
view_funccallableThe view function that the user attempted to access without permission.

Returns

TypeDescription
[HttpResponseRedirect](../../../http/response/httpresponseredirect.md?sid=django_http_response_httpresponseredirect)An HTTP redirect response pointing to the login URL with the original destination appended as a query parameter.