Skip to main content

This middleware implements compressed content encoding for HTTP. It is similar to Django’s GZipMiddleware (documentation), 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)

Summary of the project status:

Installation and usage

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

  • Python versions: 2.7, 3.5–3.11

  • Interpreters: CPython and PyPy.

  • Django versions: 1.11–4.1

pip install --upgrade django-compression-middleware

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

MIDDLEWARE = [
    # ...
    '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 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.

Further readding on Wikipedia:

Contributing

  1. Clone this repository (git clone ...)

  2. Create a virtualenv

  3. Install package dependencies: pip install --upgrade -r requirements_dev.txt

  4. Change some code

  5. Run the tests: in the project root simply execute pytest, and afterwards preferably tox to test the full test matrix. Consider installing as many supported interpreters as possible (having them in your PATH is often sufficient).

  6. Submit a pull request and check for any errors reported by the Continuous Integration service.

License

The MPL 2.0 License

Copyright (c) 2019-2023 Friedel Wolff.

Release files for django-compression-middleware 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for django-compression-middleware 0.5.0
File Size Uploaded
django-compression-middleware-0.5.0.tar.gz 20.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-compression-middleware 0.5.0
File Interpreter ABI Platform
django_compression_middleware-0.5.0-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 28.2 kB

Release files / django-compression-middleware-0.5.0.tar.gz

Download URL django-compression-middleware-0.5.0.tar.gz
Size 20.1 kB
Tags Source
SHA-256 checksum
How to use checksums
0df50f12d774659abc8bbc88e4c794f2785a8f11f30b5bb267c314b85d941b73
BLAKE2b-256 checksum
How to use checksums
fb4dda91ea4ee413802e30ff63a117022767062fdb6e3f090e73632d4fabe12f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.14

Release files / django_compression_middleware-0.5.0-py2.py3-none-any.whl

Download URL django_compression_middleware-0.5.0-py2.py3-none-any.whl
Size 8.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
9c59f56db6a5d83db50ca1c05d55f589d161062959defea41f26254e393cc37a
BLAKE2b-256 checksum
How to use checksums
2a10c5e7727529b357f6a7013610b7ba6b432ef6eb5a4d28bef0af779d19f7f2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.8.14

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page