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
zstandardbindings. It supports both a C module (for CPython) and CFFI which should be appropriate for PyPy. See the documentation for full details. - The
Brotlibindings orbrotlipy. The latter is preferred on PyPy since it is implemented using cffi. But both should work on both Python implementations. - Python's builtin
gzipmodule.
Contributing
- Clone this repository
- 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 - Change some code
- Run the tests: in the project root simply execute
uv run pytest - Submit a pull request and check for any errors reported by the Continuous Integration service.
License
The MPL 2.0 License
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 dj_compression_middleware-0.0.2.tar.gz.
File metadata
- Download URL: dj_compression_middleware-0.0.2.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6b30e14c56c9b41754063e264c2f7388771a8a06c9bac34de612829f68e166c
|
|
| MD5 |
3d887e8e7e45785e9b511f95274ee882
|
|
| BLAKE2b-256 |
7138179629b0b740995ec7284dc9b828fd728d5dc901f8bb655fd965338f384f
|
Provenance
The following attestation bundles were made for dj_compression_middleware-0.0.2.tar.gz:
Publisher:
python-package.yml on mhindery/dj-compression-middleware
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_compression_middleware-0.0.2.tar.gz -
Subject digest:
d6b30e14c56c9b41754063e264c2f7388771a8a06c9bac34de612829f68e166c - Sigstore transparency entry: 494996352
- Sigstore integration time:
-
Permalink:
mhindery/dj-compression-middleware@943f386e300725d7ac7999c33c2ec28f7fa0714b -
Branch / Tag:
refs/tags/0.0.3 - Owner: https://github.com/mhindery
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@943f386e300725d7ac7999c33c2ec28f7fa0714b -
Trigger Event:
push
-
Statement type:
File details
Details for the file dj_compression_middleware-0.0.2-py3-none-any.whl.
File metadata
- Download URL: dj_compression_middleware-0.0.2-py3-none-any.whl
- Upload date:
- Size: 7.6 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 |
35c07021487f0293af3cc2771993e526fd07d9a748c971eb6132d1049285d12e
|
|
| MD5 |
798976d2d711dff61ad93612b326fc3f
|
|
| BLAKE2b-256 |
efb626378dc12a40afbab291f697490300847f94e66ba029acb01e7e434e01a3
|
Provenance
The following attestation bundles were made for dj_compression_middleware-0.0.2-py3-none-any.whl:
Publisher:
python-package.yml on mhindery/dj-compression-middleware
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
dj_compression_middleware-0.0.2-py3-none-any.whl -
Subject digest:
35c07021487f0293af3cc2771993e526fd07d9a748c971eb6132d1049285d12e - Sigstore transparency entry: 494996381
- Sigstore integration time:
-
Permalink:
mhindery/dj-compression-middleware@943f386e300725d7ac7999c33c2ec28f7fa0714b -
Branch / Tag:
refs/tags/0.0.3 - Owner: https://github.com/mhindery
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-package.yml@943f386e300725d7ac7999c33c2ec28f7fa0714b -
Trigger Event:
push
-
Statement type: