Skip to main content
PyPI - Latest version PyPI - Python versions PyPI - Versions from Framework Classifiers

Dj Compression Middleware

Note: This project and repo was originally a fork of the project django-compression-middleware. As the project did not seem maintained anymore, I forked the project in order to get some open PR's and issues resolved. Credit goes to the original creator: Friedel Wolff. In the meantime I have refactored a lot of the code.

This package provides Django middleware to compress responses with gzip, brotli, or zstd. It is a replacement of Django's built-in GZipMiddleware as it supports more compression algorithms. Both normal and streaming responses get compressed.

Compression of responses happens at runtime, if you are looking to compress your static assets, look at e.g. Django-compressor, WhiteNoise.

The middleware looks at a requests' Accept-Encoding header in order to select appropriate compression. It will choose one using this order of preference:

  • Zstandard (zstd)
  • Brotli (br)
  • gzip (gzip)

Installation and usage

Install the package:

uv add dj-compression-middleware
# or
pip install dj-compression-middleware

Add dj_compression_middleware.middleware.CompressionMiddleware to your middleware:

MIDDLEWARE = [
    # ...
    'dj_compression_middleware.middleware.CompressionMiddleware',
    # ...
]

Remove GZipMiddleware and BrotliMiddleware if they were present, as this middleware replaces them.

Excluding views from compression

When you want to disable compression for a single view, it can be done like this for either a function-based or class-based view:

from dj_compression_middleware import no_compress, NoCompressMixin

@no_compress
def index_view(request):
    ...


class MyView(NoCompressMixin, View):
    ...

Customizing the middleware

You can subclass the middleware and customize some attributes of it to tweak its behaviour, e.g. to select the compression level:

class CustomCompressionMiddleware(CompressionMiddleware):
    # Tweak compression settings
    ZSTD_LEVEL = 7
    BROTLI_QUALITY = 4
    GZIP_COMPRESSLEVEL = 6
    ZLIB_COMPRESSLEVEL = 8

For even more customization, you can override the init method and modify the COMPRESSORS to an ordered set of your own preferred compression algorithms.

Development

Setup a virtual environment:

uv sync --frozen --all-extras --all-groups

Run linting:

uv run ruff check --no-fix
uv run ty check

Run the tests:

uv run pytest

Metadata

Release files for dj_compression_middleware 0.0.11

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

Source distribution (sdist)

Source distribution for dj_compression_middleware 0.0.11
File Size Uploaded Provenance Actions
dj_compression_middleware-0.0.11.tar.gz 9.6 kB Without provenance Details for dj_compression_middleware-0.0.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dj_compression_middleware 0.0.11
File Interpreter ABI Platform Actions
dj_compression_middleware-0.0.11-py3-none-any.whl Python 3 none any Details for dj_compression_middleware-0.0.11-py3-none-any.whl

Total release size: 19.9 kB

Release files / dj_compression_middleware-0.0.11.tar.gz

Download URL dj_compression_middleware-0.0.11.tar.gz
Size 9.6 kB
Tags Source
SHA-256 checksum
How to use checksums
4da64a3cfff45bf267fa10b67ae3870779dc87869ecad4836c0a1be657cd7e11
BLAKE2b-256 checksum
How to use checksums
95fb84b3a1e956a138fddbd76faeb21f63061302e281589b8a29b42de26ebf01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / dj_compression_middleware-0.0.11-py3-none-any.whl

Download URL dj_compression_middleware-0.0.11-py3-none-any.whl
Size 10.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f969888c62f9d26595deeb6ebc9781969eb5acebf3ac1f6ad2856be9628ebeb4
BLAKE2b-256 checksum
How to use checksums
8054be5a2b0eda9560048327aa9002f4ab70dbceb0a32a4d8e684c131b07b5af
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Security for dj_compression_middleware 0.0.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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