Skip to content

laactech/django-response-compression

Repository files navigation

Django Response Compression

Code style: black License

Combined Brotli and GZip compression for Django HTTP responses

This middleware copies Django's GZipMiddleware but adds Brotli compression. Brotli is supported by most major browsers. If the request does not allow Brotli encoding, the middleware falls back to Django's GZipMiddleware. Read Django's GZipMiddleware documentation to understand how this middleware works and heed the warnings.

Supported Versions

  • Python 3.6, 3.7, 3.8, 3.9
  • Django 2.2, 3.0, 3.1

Installation and Setup

Install via pip.

pip install django-response-compression

Add the ResponseCompressionMiddleware to your Django middleware:

MIDDLEWARE = [
    # ...
    'response_compression.middleware.ResponseCompressionMiddleware',
    # ...
]

Remove Django's GZipMiddleware if you added it.

Read the GZipMiddleware documentation for notes about middleware ordering

If you want to compress specific responses, use the decorator on your views.

from response_compression.decorators import compress_response


@compress_response
def my_view(request):
    ...

Both the middleware and decorator are compatible with Django REST Framework responses.

The default brotli compression level is 4, and you can customize the brotli compression level by adding this to django's settings:

RESPONSE_COMPRESSION_BROTLI_LEVEL = 5

Security

If you believe you've found a bug with security implications, please do not disclose this issue in a public forum.

Email us at support@laac.dev

Contribute

See CONTRIBUTING.md

About

Combined Brotli and GZip compression for Django HTTP responses

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Languages