Registry / web-framework / django-compression-middleware

django-compression-middleware

JSON →
library0.5.0pypypiunverified

Django middleware to compress HTTP responses using Brotli, Zstd, Gzip, or Deflate, with automatic content negotiation. Provides both WSGI and ASGI support. Current version 0.5.0, released sporadically.

pip install django-compression-middleware
INSTALL
IMPORT
SIG · DJANGO-COMPRESSION
D
django-compression-middleware
web-frameworkpythonv0.5.0
Install
3.7s avg
Import
Disk
89MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.000s · 84.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 3.7s · import 0.000s · 95MB
89MB installed
● package 89MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

CompressionMiddleware
from compression_middleware import CompressionMiddleware
from compression_middleware import CompressionMiddleware

Add CompressionMiddleware to MIDDLEWARE list. Place it after SecurityMiddleware but before other middleware for best performance. Optionally set COMPRESSION_ALGORITHMS and COMPRESSION_MIN_LENGTH in settings.

# settings.py MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'compression_middleware.CompressionMiddleware', # ... other middleware ] # Optional: configure compression algorithms COMPRESSION_ALGORITHMS = ['br', 'zstd', 'gzip', 'deflate'] COMPRESSION_MIN_LENGTH = 150 # bytes; default is 150
Debug
Known issues
breakingPlace middleware after SecurityMiddleware but before SessionMiddleware and CommonMiddleware. Incorrect order can break compression or security headers.
fix
Ensure order: SecurityMiddleware, CompressionMiddleware, then others.
affects: all
gotchaThe library does NOT compress responses smaller than COMPRESSION_MIN_LENGTH. Default is 150 bytes. If you expect small responses to be compressed, reduce this value.
fix
Set COMPRESSION_MIN_LENGTH = 0 in settings to compress all responses.
affects: all
gotchaOnly responses with a status code in the 200-399 range are compressed. 404, 500, etc., are skipped.
fix
Override by subclassing or adjusting middleware logic if needed.
affects: all
deprecatedIn version 0.5.0, the import path from 'compression_middleware.middleware' was removed. Must import directly from 'compression_middleware'.
fix
Use 'from compression_middleware import CompressionMiddleware'.
affects: >=0.5.0
Upgrade
Version history
0.5.0latest on PyPI · released Mar 23, 2023
Audit
Dependencies
djangorequiredRuntime requirement; middleware runs within Django request/response cycle
brotlioptionalRequired for Brotli compression (most efficient algorithm)
zstandardoptionalRequired for Zstd compression
Agent activity
6 hits · last 30 days
node
6
Resources
django-compression-middleware — pip install django-compression-middleware · libregistry