Django Compressor
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.
Warnings
- gotcha 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.
- gotcha 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.
- gotcha Nesting `{% compress %}` blocks is not supported and will result in errors.
- gotcha 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.
- gotcha 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.
Install
-
pip install django-compressor
Imports
- compress
{% load compress %} ... {% compress css %} ... {% endcompress %} - COMPRESS_ENABLED
from django.conf import settings settings.COMPRESS_ENABLED
- CompressorFinder
STATICFILES_FINDERS = [ # ... 'compressor.finders.CompressorFinder', ]
Quickstart
import os
# settings.py example
DEBUG = True # Or False, affects compression behavior
INSTALLED_APPS = [
# ... other apps
'django.contrib.staticfiles',
'compressor',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'staticfiles')
STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
'compressor.finders.CompressorFinder',
)
# COMPRESS_ENABLED defaults to not DEBUG. Set explicitly for consistent behavior.
COMPRESS_ENABLED = not DEBUG # For production, typically True
COMPRESS_OFFLINE = True # For pre-compressing assets during deployment
# Example template (e.g., base.html)
# {% load static compress %}
#
# <head>
# {% compress css %}
# <link rel="stylesheet" href="{% static 'css/base.css' %}">
# <style>body { font-family: sans-serif; }</style>
# {% endcompress %}
# </head>
# <body>
# <!-- Content -->
# {% compress js %}
# <script src="{% static 'js/main.js' %}"></script>
# <script>console.log('Hello from inline JS!');</script>
# {% endcompress %}
# </body>
# --- Steps to run ---
# 1. Add above settings to your project's settings.py.
# 2. Add the template code to one of your Django templates.
# 3. Create static/css/base.css and static/js/main.js in an app's static directory.
# 4. Run:
# python manage.py collectstatic --noinput
# python manage.py compress
# 5. Start development server:
# python manage.py runserver
# 6. View the rendered page source; you should see compressed CSS/JS links.