Package: django-compression-middleware
This middleware implements compressed content encoding for HTTP. It is similar
to Django’sGZipMiddleware(documentation), but additionally supports
other compression methods. It is meant to be a drop-in replacement for Django’sGZipMiddleware. 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 asWhiteNoise.Zstandard is a new method for compression with little client support so far.
Most browsers now support Brotli compression (check support status onCan I
use… Brotli). The middleware will choose the best compression method
supported by the client as indicated in the request’sAccept-Encodingheader. In order of preference:Zstandard (zstd)Brotli (br)gzip (gzip)Summary of the project status:Installation and usageThe following requirements are supported and tested in all reasonable
combinations:Python versions: 2.7, 3.5–3.11Interpreters: CPython and PyPy.Django versions: 1.11–4.1pipinstall--upgradedjango-compression-middlewareTo apply compression to all the views served by Django, addcompression_middleware.middleware.CompressionMiddlewareto theMIDDLEWAREsetting:MIDDLEWARE=[# ...'compression_middleware.middleware.CompressionMiddleware',# ...]RemoveGZipMiddlewareandBrotliMiddlewareif you used it before.
Consult the Django documentation on the correctordering of middleware.Alternatively you can decorate views individually to serve them with
compression:fromcompression_middleware.decoratorsimportcompress_page@compress_pagedefindex_view(request):...Note that your browser might not send thebrentry in theAccept-Encodingheader when you test without HTTPS (common on localhost). You can force it to
send the header, though. In Firefox, visitabout:configand setnetwork.http.accept-encodingto 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 ResourcesThe code and tests in this project are based on Django’sGZipMiddlewareand
Vašek Dohnal’sdjango-brotli. For compression, it uses the following modules
to bind to fast C modules:Thezstandardbindings. It supports both a C module (for CPython) and CFFI
which should be appropriate for PyPy. See the documentation for full details.TheBrotlibindings orbrotlipy. The latter is preferred on PyPy since
it is implemented using cffi. But both should work on both Python
implementations.Python’s builtingzipmodule.Further readding on Wikipedia:HTTP compressionZstandardBrotligzipContributingClone this repository (git clone ...)Create a virtualenvInstall package dependencies:pip install--upgrade-rrequirements_dev.txtChange some codeRun the tests: in the project root simply executepytest, and afterwards
preferablytoxto test the full test matrix. Consider installing as many
supported interpreters as possible (having them in yourPATHis often
sufficient).Submit a pull request and check for any errors reported by the Continuous
Integration service.LicenseThe MPL 2.0 LicenseCopyright (c) 2019-2023Friedel Wolff.