StaticFilesHandlerMixin
Common methods used by WSGI and ASGI handlers.
Attributes
| Attribute | Type | Description |
|---|---|---|
| handles_files | boolean = True | May 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
| Type | Description |
|---|---|
null | Nothing 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
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| url | string | The full URL path of the requested static resource. |
Returns
| Type | Description |
|---|---|
string | The 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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The incoming request object containing the path to be served. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The request object used to locate and serve the static file. |
Returns
| Type | Description |
|---|---|
[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
| Name | Type | Description |
|---|---|---|
| request | [HttpRequest](../../../http/request/httprequest.md?sid=django_http_request_httprequest) | The request object used to locate and serve the static file asynchronously. |
Returns
| Type | Description |
|---|---|
Coroutine | An awaitable that resolves to an HttpResponse containing the file or an error page. |