Django middleware to compress responses using several algorithms.
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.
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:
Summary of the project status:
- Supported Python versions: 2.7, 3.4, 3.5, 3.6 and 3.7. CPython and PyPy are supported.
- Supported Django versions: 1.11 (LTS), 2.0, 2.1.
pip install --upgrade django-compression-middleware
Add compression_middleware.middleware.CompressionMiddleware to the MIDDLEWARE setting:
MIDDLEWARE = [ 'compression_middleware.middleware.CompressionMiddleware', # ... ]
Remove GZipMiddleware and BrotliMiddleware if you used it before.
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 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:
- Clone this repository (git clone ...)
- Create a virtualenv
- Install package dependencies: pip install --upgrade -r requirements_dev.txt
- Change some code
- Run the tests: in the project root simply execute pytest, and afterwards preferably tox to test the full test matrix. If you don’t have all the Python interpreters, consider running tox --skip-missing-interpreters.
- Submit a pull request and check for any errors reported by the Continuous Integration service.
The MPL 2.0 License
Copyright (c) 2019 Friedel Wolff
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
|Filename, size||File type||Python version||Upload date||Hashes|
|Filename, size django_compression_middleware-0.1.0-py2.py3-none-any.whl (7.5 kB)||File type Wheel||Python version py2.py3||Upload date||Hashes View|
|Filename, size django-compression-middleware-0.1.0.tar.gz (11.5 kB)||File type Source||Python version None||Upload date||Hashes View|
Hashes for django_compression_middleware-0.1.0-py2.py3-none-any.whl
Hashes for django-compression-middleware-0.1.0.tar.gz