Skip to main content

Django middleware to compress responses with gzip, brotli and zstd.

Project description

PyPI - Latest version PyPI - Python versions PyPI - Versions from Framework Classifiers

Dj Compression Middleware

Note: This project and repo was originally a fork of the project django-compression-middleware. As the project did not seem maintained anymore, I forked the project in order to get some open PR's and issues resolved. Credit goes to the original creator: Friedel Wolff. In the meantime I have refactored a lot of the code.

This package provides Django middleware to compress responses with gzip, brotli, or zstd. It is a replacement of Django's built-in GZipMiddleware as it supports more compression algorithms. Both normal and streaming responses get compressed.

Compression of responses happens at runtime, if you are looking to compress your static assets, look at e.g. Django-compressor, WhiteNoise.

The middleware looks at a requests' Accept-Encoding header in order to select appropriate compression. It will choose one using this order of preference:

  • Zstandard (zstd)
  • Brotli (br)
  • gzip (gzip)

Installation and usage

Install the package:

uv add dj-compression-middleware
# or
pip install dj-compression-middleware

Add dj_compression_middleware.middleware.CompressionMiddleware to your middleware:

MIDDLEWARE = [
    # ...
    'dj_compression_middleware.middleware.CompressionMiddleware',
    # ...
]

Remove GZipMiddleware and BrotliMiddleware if they were present, as this middleware replaces them.

Excluding views from compression

When you want to disable compression for a single view, it can be done like this for either a function-based or class-based view:

from dj_compression_middleware import no_compress, NoCompressMixin

@no_compress
def index_view(request):
    ...


class MyView(NoCompressMixin, View):
    ...

Customizing the middleware

You can subclass the middleware and customize some attributes of it to tweak its behaviour, e.g. to select the compression level:

class CustomCompressionMiddleware(CompressionMiddleware):
    # Tweak compression settings
    ZSTD_LEVEL = 7
    BROTLI_QUALITY = 4
    GZIP_COMPRESSLEVEL = 6
    ZLIB_COMPRESSLEVEL = 8

For even more customization, you can override the init method and modify the COMPRESSORS to an ordered set of your own preferred compression algorithms.

Development

Setup a virtual environment:

uv sync --frozen --all-extras --all-groups

Run linting:

uv run ruff check --no-fix
uv run ty check

Run the tests:

uv run pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dj_compression_middleware-0.0.6.tar.gz (7.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dj_compression_middleware-0.0.6-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file dj_compression_middleware-0.0.6.tar.gz.

File metadata

  • Download URL: dj_compression_middleware-0.0.6.tar.gz
  • Upload date:
  • Size: 7.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dj_compression_middleware-0.0.6.tar.gz
Algorithm Hash digest
SHA256 54c35fa3000f225cece72ed625b9b3085fec58747e9bf71dfe5d7f283fc40874
MD5 bca16a3a4bf6bd988746a9bd1c5ebc65
BLAKE2b-256 c3b8e6cc908290ba4996c8b899f249dbebffd80cc4315bb158034f881117186f

See more details on using hashes here.

File details

Details for the file dj_compression_middleware-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: dj_compression_middleware-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 9.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.11.16 {"installer":{"name":"uv","version":"0.11.16","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for dj_compression_middleware-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9637b95153d388da6309cc2557bd44e358c75df2fd124f27712efdaf17196ff2
MD5 1987ff956efac7efac4417b65c4f78aa
BLAKE2b-256 958e79bf89e37fb99be01ec0462e7fc5f94315ca42cb7af32825cc5da3b8956d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page