{"id":23565,"library":"django-compression-middleware","title":"django-compression-middleware","description":"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.","status":"active","version":"0.5.0","language":"python","source_language":"en","source_url":"https://github.com/friedelwolff/django-compression-middleware","tags":["django","middleware","compression","brotli","zstd","gzip","deflate","asgi","wsgi"],"install":[{"cmd":"pip install django-compression-middleware","lang":"bash","label":"Install from PyPI"}],"dependencies":[{"reason":"Runtime requirement; middleware runs within Django request/response cycle","package":"django","optional":false},{"reason":"Required for Brotli compression (most efficient algorithm)","package":"brotli","optional":true},{"reason":"Required for Zstd compression","package":"zstandard","optional":true}],"imports":[{"note":"","wrong":"","symbol":"CompressionMiddleware","correct":"from compression_middleware import CompressionMiddleware"}],"quickstart":{"code":"# settings.py\nMIDDLEWARE = [\n    'django.middleware.security.SecurityMiddleware',\n    'compression_middleware.CompressionMiddleware',\n    # ... other middleware\n]\n\n# Optional: configure compression algorithms\nCOMPRESSION_ALGORITHMS = ['br', 'zstd', 'gzip', 'deflate']\nCOMPRESSION_MIN_LENGTH = 150  # bytes; default is 150","lang":"python","description":"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."},"warnings":[{"fix":"Ensure order: SecurityMiddleware, CompressionMiddleware, then others.","message":"Place middleware after SecurityMiddleware but before SessionMiddleware and CommonMiddleware. Incorrect order can break compression or security headers.","severity":"breaking","affected_versions":"all"},{"fix":"Set COMPRESSION_MIN_LENGTH = 0 in settings to compress all responses.","message":"The 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.","severity":"gotcha","affected_versions":"all"},{"fix":"Override by subclassing or adjusting middleware logic if needed.","message":"Only responses with a status code in the 200-399 range are compressed. 404, 500, etc., are skipped.","severity":"gotcha","affected_versions":"all"},{"fix":"Use 'from compression_middleware import CompressionMiddleware'.","message":"In version 0.5.0, the import path from 'compression_middleware.middleware' was removed. Must import directly from 'compression_middleware'.","severity":"deprecated","affected_versions":">=0.5.0"}],"env_vars":null,"last_verified":"2026-05-01T00:00:00.000Z","next_check":"2026-07-30T00:00:00.000Z","problems":[{"fix":"Change import to 'from compression_middleware import CompressionMiddleware'.","cause":"In version 0.5.0, the module path changed. The old import 'compression_middleware.middleware' is gone.","error":"ImportError: cannot import name 'CompressionMiddleware' from 'compression_middleware.middleware'"},{"fix":"Do not subclass or override process_response. Instead, use the middleware as-is.","cause":"The middleware is implemented differently (uses __call__ for ASGI/WSGI compat). Trying to override process_response will not work.","error":"AttributeError: 'CompressionMiddleware' object has no attribute 'process_response'"},{"fix":"Use the dotted string path: 'compression_middleware.CompressionMiddleware' (not an instance).","cause":"Using the middleware class directly in MIDDLEWARE setting without parentheses (old-style) but the class expects no arguments.","error":"TypeError: __init__() got an unexpected keyword argument 'get_response'"}],"ecosystem":"pypi","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}