Collectfast: A Faster Collectstatic for Django

2.2.0 · active · verified Thu Apr 16

Collectfast dramatically speeds up Django's `collectstatic` command when using cloud storage by only uploading static files that have changed. It leverages MD5 hashes (ETags) or modification times to determine if a file needs to be re-uploaded. The current version is 2.2.0, with a release cadence of a few times per year, focusing on compatibility and performance improvements.

Common errors

Warnings

Install

Imports

Quickstart

To quickly set up `collectfast`, add it to your `INSTALLED_APPS` and configure the necessary settings in your Django `settings.py`. Ensure `COLLECTFAST_ENABLED` is `True` and, crucially, set `COLLECTFAST_STRATEGY` to match your static files storage backend. This example shows configuration for AWS S3 using `django-storages`.

import os

# settings.py

# Add collectfast to your INSTALLED_APPS
INSTALLED_APPS = [
    # ... other apps
    'collectfast',
    'storages', # if using django-storages
]

# Enable Collectfast
COLLECTFAST_ENABLED = True

# Explicitly set the strategy (required since collectfast 2.0.0)
# Choose the strategy that matches your STATICFILES_STORAGE backend
# For AWS S3 with django-storages s3boto3 backend:
COLLECTFAST_STRATEGY = 'collectfast.strategies.aws.S3Strategy'

# Configure your STATICFILES_STORAGE (example for AWS S3 with django-storages)
AWS_ACCESS_KEY_ID = os.environ.get('AWS_ACCESS_KEY_ID', 'your_access_key')
AWS_SECRET_ACCESS_KEY = os.environ.get('AWS_SECRET_ACCESS_KEY', 'your_secret_key')
AWS_STORAGE_BUCKET_NAME = os.environ.get('AWS_STORAGE_BUCKET_NAME', 'your-bucket-name')
AWS_S3_REGION_NAME = os.environ.get('AWS_S3_REGION_NAME', 'us-east-1')
AWS_S3_SIGNATURE_VERSION = 's3v4'
AWS_S3_FILE_OVERWRITE = False
AWS_DEFAULT_ACL = None # Or 'public-read' if files should be publicly accessible

STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'
STATIC_URL = f"https://{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com/"

# Define STATIC_ROOT for local collectstatic (even if collectfast bypasses it for cloud storage)
STATIC_ROOT = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'staticfiles')

view raw JSON →