Skip to main content

response_for_exception

Generates an appropriate HTTP response for a given exception, handling specific error types like 404, 403, and 400 while logging the events and rendering template responses. It triggers the got_request_exception signal for uncaught exceptions and provides technical debug responses when settings.DEBUG is enabled.

def response_for_exception(
request: HttpRequest,
exc: Exception
) - > HttpResponse

Generates an appropriate HTTP response based on the type of exception raised during request processing. It handles specific Django exceptions like Http404 or PermissionDenied by returning themed error pages and logs the failure details.

Parameters

NameTypeDescription
requestHttpRequestThe current web request object used to determine the context for the error response and logging.
excExceptionThe exception instance that was raised, which determines the status code and template used for the response.

Returns

TypeDescription
HttpResponseA fully rendered HTTP response object corresponding to the exception type, such as a 404, 403, 400, or 500 error page.