Middleware

class static_delivery.middleware.StaticDeliveryMiddleware(get_response=None)[source]

Bases: object

Middleware to serve static files from within Django.

In some setups it is a good idea to serve static files from Django and have them cached in a reverse proxy like Nginx or something similar.

By doing this, we can easily serve static files from our - for example - our Docker image without putting them in a shared volume.

It is important to know that serving files from Django directly won’t perform very well. Always have a cache in front of it.

Additionally, the middleware is able to recover from invalid hashes in static file names when you use a staticfiles storage with name hashing in place. If a file with a certain hash is unavailable, the middleware will try to look up the correct hash for the file.

HASHED_PATH_RE = re.compile('(.+)(\\.[0-9a-f]{12})(\\.?)(\\w+)?$')[source]
path_re = None[source]

The middleware instance has a regex ready to match paths against STATIC_URL.

manifest = None[source]

the staicfiles manifest is loaded once when the middleware is initialized.

serve_response(request, path)[source]

This method takes the request and a path to deliver static content for.

The method tries to deliver content for the requested path, if this fails the code will try to recover the currently valid path and try to serve that file instead. If nothing works, None is returned.

get_staticfile_response(request, path)[source]

This method takes a path and tries to serve the content for the given path. Will return None if serving fails.

recover_staticfile_path(path)[source]

This method strips the hash from the requested path and tries to look up the unhashed file name in the manifest dataset.

load_staticfiles_manifest()[source]

Supported staticfiles storages map original static file names to hashed ones. This method loads the manifest file for lookups later.

In addition, this method might raise an exception if the configured staticfiles storage doesn’t support manifest files/data.