Skip to main content

StaticFilesHandlerMixin

Common methods used by WSGI and ASGI handlers.

Attributes

AttributeTypeDescription
handles_filesboolean = TrueMay be used to differentiate between handler types (e.g. in a request_finished signal)

Methods


load_middleware()

@classmethod
def load_middleware() - > null

Skips middleware loading as they are already initialized for the underlying application instance.

Returns

TypeDescription
nullNothing is returned.

get_base_url()

@classmethod
def get_base_url() - > string

Validates static file settings and retrieves the configured base URL for static assets.

Returns

TypeDescription
stringThe STATIC_URL string defined in the application settings.

file_path()

@classmethod
def file_path(
url: string
) - > string

Return the relative path to the media file on disk for the given URL.

Parameters

NameTypeDescription
urlstringThe full URL path of the requested static resource.

Returns

TypeDescription
stringThe OS-specific filesystem path relative to the static root directory.

serve()

@classmethod
def serve(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > [HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)

Serve the request path.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The incoming request object containing the path to be served.

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The file content wrapped in an HTTP response, served in insecure mode.

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)

Attempts to serve the static file and returns a formatted error response if the file is not found.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The request object used to locate and serve the static file.

Returns

TypeDescription
[HttpResponse](../../../http/response/httpresponse.md?sid=django_http_response_httpresponse)The static file response or a standard error response for 404 exceptions.

get_response_async()

@classmethod
def get_response_async(
request: [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)
) - > Coroutine

Asynchronously attempts to serve the static file, handling 404 exceptions in a thread-safe manner.

Parameters

NameTypeDescription
request[HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest)The request object used to locate and serve the static file asynchronously.

Returns

TypeDescription
CoroutineAn awaitable that resolves to an HttpResponse containing the file or an error page.