{"id":3330,"library":"django-compressor","title":"Django Compressor","description":"Django Compressor is a reusable Django application that processes, combines, and minifies linked and inline JavaScript and CSS within Django templates into cacheable static files. It supports various compilers like CoffeeScript, LESS, and SASS, and offers extensibility for custom processing steps. As of version 4.6.0, it remains actively maintained with a regular release cadence, ensuring compatibility with recent Django versions.","status":"active","version":"4.6.0","language":"en","source_language":"en","source_url":"https://github.com/django-compressor/django-compressor","tags":["django","css","javascript","compression","minification","assets","static-files"],"install":[{"cmd":"pip install django-compressor","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Used internally for handling Django settings configuration, automatically installed.","package":"django-appconf","optional":false},{"reason":"Default CSS minifier.","package":"rcssmin","optional":false},{"reason":"Default JavaScript minifier.","package":"rjsmin","optional":false},{"reason":"Optional parser: compressor.parser.BeautifulSoupParser.","package":"beautifulsoup4","optional":true},{"reason":"Optional parser: compressor.parser.LxmlParser (requires libxml2).","package":"lxml","optional":true},{"reason":"Optional parser: compressor.parser.Html5LibParser.","package":"html5lib","optional":true},{"reason":"Optional JavaScript filter: compressor.filters.jsmin.CalmjsFilter.","package":"calmjs","optional":true},{"reason":"Optional CSS filter: compressor.filters.cssmin.CSSCompressorFilter.","package":"csscompressor","optional":true},{"reason":"Optional for compressor.storage.BrotliCompressorFileStorage.","package":"brotli","optional":true},{"reason":"Optional for including template code into main template.","package":"django-sekizai","optional":true},{"reason":"Optional for Jinja2 templating support.","package":"jinja2","optional":true}],"imports":[{"note":"Primary usage is through a Django template tag.","symbol":"compress","correct":"{% load compress %} ... {% compress css %} ... {% endcompress %}"},{"note":"Access configuration settings from django.conf.settings.","symbol":"COMPRESS_ENABLED","correct":"from django.conf import settings\nsettings.COMPRESS_ENABLED"},{"note":"Must be added to STATICFILES_FINDERS for staticfiles integration.","symbol":"CompressorFinder","correct":"STATICFILES_FINDERS = [\n    # ...\n    'compressor.finders.CompressorFinder',\n]"}],"quickstart":{"code":"import os\n\n# settings.py example\n\nDEBUG = True # Or False, affects compression behavior\n\nINSTALLED_APPS = [\n    # ... other apps\n    'django.contrib.staticfiles',\n    'compressor',\n]\n\nSTATIC_URL = '/static/'\nSTATIC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'staticfiles')\n\nSTATICFILES_FINDERS = (\n    'django.contrib.staticfiles.finders.FileSystemFinder',\n    'django.contrib.staticfiles.finders.AppDirectoriesFinder',\n    'compressor.finders.CompressorFinder',\n)\n\n# COMPRESS_ENABLED defaults to not DEBUG. Set explicitly for consistent behavior.\nCOMPRESS_ENABLED = not DEBUG # For production, typically True\nCOMPRESS_OFFLINE = True # For pre-compressing assets during deployment\n\n# Example template (e.g., base.html)\n# {% load static compress %}\n#\n# <head>\n#     {% compress css %}\n#         <link rel=\"stylesheet\" href=\"{% static 'css/base.css' %}\">\n#         <style>body { font-family: sans-serif; }</style>\n#     {% endcompress %}\n# </head>\n# <body>\n#     <!-- Content -->\n#     {% compress js %}\n#         <script src=\"{% static 'js/main.js' %}\"></script>\n#         <script>console.log('Hello from inline JS!');</script>\n#     {% endcompress %}\n# </body>\n\n# --- Steps to run ---\n# 1. Add above settings to your project's settings.py.\n# 2. Add the template code to one of your Django templates.\n# 3. Create static/css/base.css and static/js/main.js in an app's static directory.\n# 4. Run:\n#    python manage.py collectstatic --noinput\n#    python manage.py compress\n# 5. Start development server:\n#    python manage.py runserver\n# 6. View the rendered page source; you should see compressed CSS/JS links.","lang":"python","description":"To quickly integrate Django Compressor, add 'compressor' to `INSTALLED_APPS` and 'compressor.finders.CompressorFinder' to `STATICFILES_FINDERS`. Define `STATIC_URL` and `STATIC_ROOT`. Use the `{% load compress %}` and `{% compress css %}`/`{% compress js %}` template tags around your static and inline assets. For production, set `COMPRESS_ENABLED = True` and typically `COMPRESS_OFFLINE = True`, then run `python manage.py collectstatic` followed by `python manage.py compress` during deployment to pre-process assets."},"warnings":[{"fix":"For consistent testing of compressed output in development, explicitly set `COMPRESS_ENABLED = True` in your settings. For production, ensure `COMPRESS_ENABLED = True` and consider `COMPRESS_OFFLINE = True`.","message":"The `COMPRESS_ENABLED` setting (defaulting to `not DEBUG`) significantly alters behavior. If `DEBUG` is True and `COMPRESS_ENABLED` is also True, inaccessible or non-existent linked files within `{% compress %}` blocks will raise exceptions. If `DEBUG` is False, these files are silently stripped. Always test compression behavior explicitly, especially when `DEBUG` is True.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure all template context variables needed within `{% compress %}` blocks during offline compression are added to the `COMPRESS_OFFLINE_CONTEXT` dictionary in your settings. For example: `COMPRESS_OFFLINE_CONTEXT = {'STATIC_URL': '/static/', 'APP_VERSION': '1.0'}`.","message":"When using `COMPRESS_OFFLINE = True` (recommended for production), any Django template context variables used inside `{% compress %}` blocks will *not* be available during the `manage.py compress` command execution unless explicitly defined in `COMPRESS_OFFLINE_CONTEXT`. This can lead to `OfflineGenerationError` or incorrect output if variables are missing.","severity":"gotcha","affected_versions":"All versions with `COMPRESS_OFFLINE = True`"},{"fix":"Avoid placing one `{% compress %}` block inside another. Refactor your templates to ensure `compress` blocks are self-contained.","message":"Nesting `{% compress %}` blocks is not supported and will result in errors.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure your deployment script or process runs `python manage.py collectstatic --noinput` followed by `python manage.py compress`.","message":"When using offline compression (`COMPRESS_OFFLINE = True`), the `manage.py collectstatic` command should be run *before* `manage.py compress`. Running them in the reverse order can cause `compress` to not find the static files it needs to process, resulting in uncompressed assets or errors.","severity":"gotcha","affected_versions":"All versions with `COMPRESS_OFFLINE = True`"},{"fix":"Configure `CACHES` in `settings.py` for a production-grade cache (e.g., `django-redis`) and set `COMPRESS_CACHE_BACKEND = 'your_cache_alias'`.","message":"For optimal production performance, it is strongly recommended to configure a robust Django cache backend (e.g., Memcached or Redis) and point `COMPRESS_CACHE_BACKEND` to it. The default filesystem cache might lead to performance bottlenecks, especially with frequent checks of compressed files.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-11T00:00:00.000Z","next_check":"2026-07-10T00:00:00.000Z"}