Skip to main content

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

Project description

Dj Compression Middleware

Note: This repo is 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. Most of the credit goes to the original creator: Friedel Wolff.

This middleware implements compressed content encoding for HTTP. It is similar to Django's GZipMiddleware but additionally supports other compression methods. It is meant to be a drop-in replacement for Django's GZipMiddleware. Its documentation — including security warnings — therefore apply here as well.

The middleware is focussed on the task of compressing typical Django responses such as HTML, JSON, etc. Both normal (bulk) and streaming responses are supported. For static file compression, have a look at other projects such as WhiteNoise_.

Zstandard is a new method for compression with little client support so far. Most browsers now support Brotli compression (check support status on Can I use... Brotli_). The middleware will choose the best compression method supported by the client as indicated in the request's Accept-Encoding header. In order of preference:

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

Installation and usage

The following requirements are supported and tested in all reasonable combinations:

  • Python versions: 3.10–3.14
  • Django versions: 4.0–5.2

Add the package to your project, e.g.

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

To apply compression to all the views served by Django, add dj_compression_middleware.middleware.CompressionMiddleware to the MIDDLEWARE setting:

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

Remove GZipMiddleware and BrotliMiddleware if you used it before. Consult the Django documentation on the correct ordering of middleware

Alternatively you can decorate views individually to serve them with compression:

from dj_compression_middleware.decorators import compress_page

@compress_page
def index_view(request):
    ...

Note that your browser might not send the br entry in the Accept-Encoding header when you test without HTTPS (common on localhost). You can force it to send the header, though. In Firefox, visit about:config and set network.http.accept-encoding to indicate support. Note that you might encounter some problems on the web with such a setting (which is why Brotli is only supported on secure connections by default).

Credits and Resources

The code and tests in this project are based on Django's GZipMiddleware and Vašek Dohnal's django-brotli. For compression, it uses the following modules to bind to fast C modules:

  • The zstandard bindings. It supports both a C module (for CPython) and CFFI which should be appropriate for PyPy. See the documentation for full details.
  • The Brotli bindings or brotlipy. The latter is preferred on PyPy since it is implemented using cffi. But both should work on both Python implementations.
  • Python's builtin gzip module.

Contributing

  1. Clone this repository
  2. Setup an environment using uv: uv sync --python-preference only-managed --python 3.12 --frozen --compile-bytecode --all-extras --group dev --group tests --group pages
  3. Change some code
  4. Run the tests: in the project root simply execute uv run pytest
  5. Submit a pull request and check for any errors reported by the Continuous Integration service.

License

The MPL 2.0 License

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.4.tar.gz (7.1 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.4-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for dj_compression_middleware-0.0.4.tar.gz
Algorithm Hash digest
SHA256 fd4fc52cacb3af9addce3d81c4340cd439859b660fb26007cf1e5983fed8f62a
MD5 5d1d08a7d3a854c0cff8340fa7bcfd90
BLAKE2b-256 ff77ffa81da4d9ab905707af18cf83a9cbd960c423ea8ae1bf8a9acfbdf0bc9e

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_compression_middleware-0.0.4.tar.gz:

Publisher: python-package.yml on mhindery/dj-compression-middleware

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

File hashes

Hashes for dj_compression_middleware-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 f19a42ea868b421c69e8d4057e2b6fa53ee1eaf529278444aa98ec269d399c8c
MD5 8fba0c0fb48168d584c95892b2bfeb59
BLAKE2b-256 33a0e99ed2aeceb663ffc929842ffb1bcd494dfba8ab5f59d57b2cbb89fa781c

See more details on using hashes here.

Provenance

The following attestation bundles were made for dj_compression_middleware-0.0.4-py3-none-any.whl:

Publisher: python-package.yml on mhindery/dj-compression-middleware

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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