Django middleware for compressing HTTP responses with Zstandard, Brotli, or Gzip.
Project description
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.9 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) - on Python 3.14+.
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 (on Python 3.14+).
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.
For the Gzip coding, the middleware mitigates some attacks using the Heal the Breach (HTB) technique, as used in Django’s GzipMiddleware. This fix adds a small number of random bytes to each response. To change the maximum number of random bytes added to responses, subclass the middleware and change the gzip_max_random_bytes attribute appropriately (default 100).
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_http_compression-1.2.0.tar.gz.
File metadata
- Download URL: django_http_compression-1.2.0.tar.gz
- Upload date:
- Size: 17.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c811e93e5c6b8798612cdab2a971b1630799dba137973823b48e02d80019cec9
|
|
| MD5 |
ff8cff63691d2a378fdf53e0bc90b1ee
|
|
| BLAKE2b-256 |
6aa96efa8bf41a316d02004628afc555fe9a80acee012709dd40d8e348f446c0
|
Provenance
The following attestation bundles were made for django_http_compression-1.2.0.tar.gz:
Publisher:
main.yml on adamchainz/django-http-compression
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_http_compression-1.2.0.tar.gz -
Subject digest:
c811e93e5c6b8798612cdab2a971b1630799dba137973823b48e02d80019cec9 - Sigstore transparency entry: 641944851
- Sigstore integration time:
-
Permalink:
adamchainz/django-http-compression@70a56d8c3e8f96b97a86479cf757dc5849343f2a -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@70a56d8c3e8f96b97a86479cf757dc5849343f2a -
Trigger Event:
push
-
Statement type:
File details
Details for the file django_http_compression-1.2.0-py3-none-any.whl.
File metadata
- Download URL: django_http_compression-1.2.0-py3-none-any.whl
- Upload date:
- Size: 9.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dcde57e7d0103e3859201e640b06108893407c06da021391fe77ffca2cf8bbd0
|
|
| MD5 |
339c6fa03499aa5ecaed2de13b43bdab
|
|
| BLAKE2b-256 |
9578422fa3db2969ce7ad0789b5a0112187d9d5115f4c54bae14625a2ffb7e4d
|
Provenance
The following attestation bundles were made for django_http_compression-1.2.0-py3-none-any.whl:
Publisher:
main.yml on adamchainz/django-http-compression
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
django_http_compression-1.2.0-py3-none-any.whl -
Subject digest:
dcde57e7d0103e3859201e640b06108893407c06da021391fe77ffca2cf8bbd0 - Sigstore transparency entry: 641944854
- Sigstore integration time:
-
Permalink:
adamchainz/django-http-compression@70a56d8c3e8f96b97a86479cf757dc5849343f2a -
Branch / Tag:
refs/tags/1.2.0 - Owner: https://github.com/adamchainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
main.yml@70a56d8c3e8f96b97a86479cf757dc5849343f2a -
Trigger Event:
push
-
Statement type: