Skip to main content

BaseHandler

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

NameTypeDescription
is_asyncboolean = FalseDetermines whether to load the middleware chain in asynchronous mode.

Returns

TypeDescription
nullnull

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

NameTypeDescription
is_asyncbooleanThe target execution mode; True for asynchronous, False for synchronous.
methodcallableThe function or method to be adapted.
method_is_asyncboolean = nullExplicitly defines if the input method is async; if None, it is detected automatically.
debugboolean = FalseEnables logging of adaptation actions for debugging purposes.
namestring = nullA descriptive name for the method used in debug log messages.

Returns

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

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request to be processed.

Returns

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

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming HTTP request to be processed asynchronously.

Returns

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

NameTypeDescription
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

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

NameTypeDescription
responseanyThe object returned by a view or middleware to be validated.
callbackcallableThe view or middleware function that produced the response, used for error reporting.
namestring = nullAn optional explicit name for the callback to use in the exception message.

Returns

TypeDescription
nullnull

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

NameTypeDescription
viewcallableThe view function to potentially wrap in a transaction.

Returns

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

NameTypeDescription
exceptionExceptionThe 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

TypeDescription
`HttpResponsenull`