LoginRequiredMiddleware
Middleware that redirects all unauthenticated requests to a login page.
Attributes
| Attribute | Type | Description |
|---|---|---|
| redirect_field_name | string = REDIRECT_FIELD_NAME | The 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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current web request object being processed. |
| view_func | callable | The Python function or class-based view that Django is about to execute. |
| view_args | tuple | Positional arguments that will be passed to the view function. |
| view_kwargs | dict | Keyword arguments that will be passed to the view function. |
Returns
| Type | Description |
|---|---|
| `HttpResponse | None` |
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
| Name | Type | Description |
|---|---|---|
| view_func | callable | The view function being accessed, used to check for a view-specific login URL override. |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| view_func | callable | The view function being accessed, used to check for a view-specific redirect field name override. |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The current web request object used to determine the original destination path. |
| view_func | callable | The view function that the user attempted to access without permission. |
Returns
| Type | Description |
|---|---|
[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. |