Skip to main content

Django middleware to compress responses using several algorithms.

Project description

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:

  • Brotli

  • 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.4, 3.5, 3.6, 3.7.

  • Interpreters: CPython and PyPy.

  • Django versions: 1.11 (LTS), 2.0, 2.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 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 Friedel Wolff

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-compression-middleware-0.2.1.tar.gz (17.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_compression_middleware-0.2.1-py2.py3-none-any.whl (7.3 kB view details)

Uploaded Python 2Python 3

File details

Details for the file django-compression-middleware-0.2.1.tar.gz.

File metadata

  • Download URL: django-compression-middleware-0.2.1.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for django-compression-middleware-0.2.1.tar.gz
Algorithm Hash digest
SHA256 6c5e8d57efbaa4bc8a05de527e718eb9ab402df094ca0e10c6ecf1e0d75f409a
MD5 730761cf5b73013f579e597370916410
BLAKE2b-256 0687b8c5d36e312677ea4477d90963809abf21042bbd2e35d50d100dd2bc1946

See more details on using hashes here.

File details

Details for the file django_compression_middleware-0.2.1-py2.py3-none-any.whl.

File metadata

  • Download URL: django_compression_middleware-0.2.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 7.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.30.0 CPython/3.7.2

File hashes

Hashes for django_compression_middleware-0.2.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ba6aa6d580861995a1edb3bf319b078e9554e13f53c6da888614c54cb00e0531
MD5 70ab0598219fc81b6f7bac769782c2c3
BLAKE2b-256 66ba8f3a566345c206c00b98e9199c29245255dbc5ec71eadb1a302760d678bb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page