This class provides the base logic for handling HTTP requests and generating responses by managing a chain of middleware and resolving views. It supports both synchronous and asynchronous execution modes, automatically adapting middleware and view methods to match the required calling convention. Key capabilities include loading middleware from settings, processing template responses, and handling exceptions through a dedicated middleware stack.
Methods
load_middleware()
@classmethod
def load_middleware(
is_async: boolean = False
) - > null
Populate middleware lists from settings.MIDDLEWARE. Must be called after the environment is fixed (see call in subclasses).
Parameters
| Name | Type | Description |
|---|
| is_async | boolean = False | Determines whether to load the middleware chain in asynchronous mode. |
Returns
adapt_method_mode()
@classmethod
def adapt_method_mode(
is_async: boolean,
method: callable,
method_is_async: boolean = null,
debug: boolean = False,
name: string = null
) - > callable
Adapt a method to be in the correct "mode": If is_async is False, asynchronous methods are wrapped with async_to_sync; if is_async is True, synchronous methods are wrapped with sync_to_async().
Parameters
| Name | Type | Description |
|---|
| is_async | boolean | The target execution mode; True for asynchronous, False for synchronous. |
| method | callable | The function or method to be adapted. |
| method_is_async | boolean = null | Explicitly defines if the input method is async; if None, it is detected automatically. |
| debug | boolean = False | Enables logging of adaptation actions for debugging purposes. |
| name | string = null | A descriptive name for the method used in debug log messages. |
Returns
| Type | Description |
|---|
callable | The adapted version of the input method, wrapped for the target execution mode if necessary. |
get_response()
@classmethod
def get_response(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Return an HttpResponse object for the given HttpRequest.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming HTTP request to be processed. |
Returns
| Type | Description |
|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | The final response generated by the middleware chain and the resolved view. |
get_response_async()
@classmethod
def get_response_async(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)
Asynchronous version of get_response. Funneling everything, including WSGI, into a single async get_response() is too slow. Avoid the context switch by using a separate async response path.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming HTTP request to be processed asynchronously. |
Returns
| Type | Description |
|---|
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse) | A coroutine that resolves to the final HttpResponse object. |
resolve_request()
@classmethod
def resolve_request(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > [ResolverMatch](../../../urls/resolvers/resolvermatch.md?sid=django_urls_resolvers_resolvermatch)
Retrieve/set the urlconf for the request. Return the view resolved, with its args and kwargs.
Parameters
| Name | Type | Description |
|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The request object used to resolve the URL path to a specific view. |
Returns
| Type | Description |
|---|
[ResolverMatch](../../../urls/resolvers/resolvermatch.md?sid=django_urls_resolvers_resolvermatch) | The resolved URL match containing the view function and extracted arguments. |
check_response()
@classmethod
def check_response(
response: any,
callback: callable,
name: string = null
) - > null
Raise an error if the view returned None or an uncalled coroutine.
Parameters
| Name | Type | Description |
|---|
| response | any | The object returned by a view or middleware to be validated. |
| callback | callable | The view or middleware function that produced the response, used for error reporting. |
| name | string = null | An optional explicit name for the callback to use in the exception message. |
Returns
make_view_atomic()
@classmethod
def make_view_atomic(
view: callable
) - > callable
Wraps a view in a database transaction if ATOMIC_REQUESTS is enabled for the database connection. Raises a RuntimeError if an async view is used with atomic requests.
Parameters
| Name | Type | Description |
|---|
| view | callable | The view function to potentially wrap in a transaction. |
Returns
| Type | Description |
|---|
callable | The original view or a view wrapped in a transaction.atomic decorator. |
process_exception_by_middleware()
@classmethod
def process_exception_by_middleware(
exception: Exception,
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > HttpResponse | null
Pass the exception to the exception middleware. If no middleware return a response for this exception, return None.
Parameters
| Name | Type | Description |
|---|
| exception | Exception | The exception instance raised during request processing or view execution. |
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The request object associated with the raised exception. |
Returns
| Type | Description |
|---|
| `HttpResponse | null` |