Django middleware for compressing HTTP responses with Zstandard, Brotli, or Gzip.
For a bit of background, see the introductory blog post.
Work smarter and faster with my book Boost Your Django DX which covers many ways to improve your development experience.
Requirements
Python 3.10 to 3.14 supported.
Django 4.2 to 6.0 supported.
From Python 3.14, Zstandard support requires libzstd to be linked into Python. (uv’s Python distributions include it on Unix.)
Installation
Install with pip:
python -m pip install django-http-compression
To include Brotli support, add the brotli extra to pull in the brotli package:
python -m pip install 'django-http-compression[brotli]'Most browsers support Zstandard (MDN), but you may want to include Brotli as an option for clients that do not.
Add django-http-compression to your INSTALLED_APPS:
INSTALLED_APPS = [ ..., "django_http_compression", ..., ]Add the middleware:
MIDDLEWARE = [ ..., "django_http_compression.middleware.HttpCompressionMiddleware", ..., ]The middleware should be above any that may modify your HTML, such as those of django-debug-toolbar or django-browser-reload. Remove any other middleware that will encode your responses, such as Django’s GZipMiddleware.
API
django_http_compression.middleware.HttpCompressionMiddleware
This middleware is similar to Django’s GZipMiddleware, but with extra coding support. It compresses responses with one of three codings, depending on what the client supports per the request’s Accept-Encoding header:
Zstandard (zstd).
Brotli (br), if the brotli extra is installed.
Gzip (gzip).
See the MDN content-encoding documentation for more details on these codings and their browser support.
Codings are prioritized based on any q factors sent by the client, for example accept-encoding: gzip;q=1.0, br;q=0.9 will prefer gzip over Brotli. After that, the middleware prefers the algorithms in the order of the above list, with Zstandard being the most preferred. This is because it’s the most performant, offering similar compression to Brotli in about half the time (per CloudFlare’s testing).
In practice, modern browsers do not send q factors and nearly all support Zstandard, so that will generally be selected.
The middleware skips compression if any of the following are true:
The content-type header does not match a known-compressible type like text/html.
The content body is less than 50 bytes.
The response already has a Content-Encoding header.
The request does not have a supported accept-encoding header.
Compression lengthens the response (for non-streaming responses).
If the response has an etag header, the etag is made weak to comply with RFC 9110 Section 8.8.1.
The middleware mitigates some attacks using the Heal the Breach (HTB) technique, like Django’s GzipMiddleware does. This technique adds some random padding to responses: an x-noise header with a random token containing between 0 and 99 bytes long, Base64-encoded. This padding makes it much harder for attackers to infer the contents of the response by measuring its compressed size while modifying some input, for example by trying to guess an embedded session token character-by-character. To change the maximum number of random bytes added to responses, subclass the middleware and set the max_random_bytes attribute.
History
Django has supported Gzip compression since before version 1.0, from this commit (2005). Since then, compression on the web has evolved in Brotli (2013) and Zstandard (2015), with browsers adding support for both.
Brotli support on Python has always required a third-party package, making it a little inconvenient. But with Python 3.14 adding Zstandard support to the standard library, it’s much easier to support a modern, efficient compression algorithm.
This project exists as an evolution of Django’s GZipMiddleware, with the aim to provide a base for adding (at least) Zstandard support to Django itself. It pulls inspiration from the django-compression-middleware package.
Metadata
Release files for django-http-compression 1.4.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| django_http_compression-1.4.0.tar.gz | 18.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| django_http_compression-1.4.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.1 kB
Release files / django_http_compression-1.4.0.tar.gz
| Download URL | django_http_compression-1.4.0.tar.gz |
|---|---|
| Size | 18.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6201c8908917b5ccbd4eb46a0f12840413e869c66ba9a7a38988b99ad9854ba0
|
|
BLAKE2b-256 checksum How to use checksums |
6d084acb01609bba92b6202065336934ff9885c7078586974cc5b4b8f39be3d4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 8, 2026.
Transparency logRelease files / django_http_compression-1.4.0-py3-none-any.whl
| Download URL | django_http_compression-1.4.0-py3-none-any.whl |
|---|---|
| Size | 10.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
98b39d7040e8e2a03a87f0997fb126bb867f5eba0ef883d585f8889cec18825d
|
|
BLAKE2b-256 checksum How to use checksums |
07aa75680f5fde48795e8650575ffbe04ced38936caa6d034b316df4f1b107e9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Apr 8, 2026.
Transparency log