Collect Static Files

Gathering all static files for production deployment

📦 What is collectstatic?

The collectstatic command gathers all static files from your apps and specified directories into a single location for production. This makes serving static files efficient and organized on live servers.


# Collect all static files into STATIC_ROOT
python manage.py collectstatic
                                    

Why Collect Static Files?

In production, Django doesn't serve static files automatically like in development. Collecting static files centralizes them in one directory for efficient serving by web servers or CDNs.

🎯

Centralized

All files in one location

staticfiles/
🚀

Production Ready

Optimized for deployment

Compressed & cached
🔄

Auto-Gather

Finds files from all apps

Multiple sources

Version Control

Handles file updates

Overwrites old files

🔹 Configure STATIC_ROOT

Set where collected files should be stored:

# settings.py
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

# URL prefix for static files
STATIC_URL = '/static/'

# Directory where collectstatic will gather files
STATIC_ROOT = BASE_DIR / 'staticfiles'

# Additional locations of static files
STATICFILES_DIRS = [
    BASE_DIR / 'static',  # Project-level static folder
]

📁 Folder Structure:

myproject/
├── static/              # Your static files
├── staticfiles/         # Collected files (created by collectstatic)
├── myapp/
│   └── static/
└── manage.py

🔹 Run collectstatic Command

Execute the command to gather all static files:

# Basic command
python manage.py collectstatic

# Skip confirmation prompt
python manage.py collectstatic --noinput

# Clear existing files first
python manage.py collectstatic --clear --noinput

Output:

You have requested to collect static files at the destination
location as specified in your settings:

    /home/user/myproject/staticfiles

This will overwrite existing files!
Are you sure you want to do this?

Type 'yes' to continue, or 'no' to cancel: yes

128 static files copied to '/home/user/myproject/staticfiles'.

🔹 What Gets Collected?

collectstatic gathers files from multiple sources:

Sources:

  1. App static folders: myapp/static/
  2. STATICFILES_DIRS: Directories you specify
  3. Django admin: Built-in admin CSS/JS
  4. Third-party packages: Installed app static files
# Example of collected structure
staticfiles/
├── admin/              # Django admin files
│   ├── css/
│   └── js/
├── myapp/              # Your app files
│   ├── css/
│   │   └── style.css
│   └── js/
│       └── script.js
└── css/                # Project-level files
    └── global.css

🔹 Handle File Conflicts

When multiple apps have files with the same name:

# Django uses the first file it finds based on INSTALLED_APPS order

# settings.py
INSTALLED_APPS = [
    'myapp',      # Files from myapp take priority
    'otherapp',   # Files from otherapp used if not in myapp
]

# Best practice: Namespace your static files
# myapp/static/myapp/css/style.css  ✅
# myapp/static/css/style.css        ❌ (can conflict)

⚠️ Warning:

If two apps have static/css/style.css, only one will be collected. Always namespace your static files!

🔹 Ignore staticfiles in Git

Don't commit collected files to version control:

# .gitignore
# Django static files
/staticfiles/
/static_root/

# Media files
/media/

# Python
*.pyc
__pycache__/
*.py[cod]

# Virtual environment
venv/
env/

Why ignore staticfiles?

  • Generated automatically during deployment
  • Keeps repository clean and small
  • Prevents merge conflicts
  • Each environment generates its own

🔹 Automate in Deployment

Run collectstatic automatically when deploying:

# In your deployment script or Procfile
# Heroku example (Procfile)
release: python manage.py collectstatic --noinput
web: gunicorn myproject.wsgi

# Railway example (railway.json)
{
  "build": {
    "builder": "NIXPACKS"
  },
  "deploy": {
    "startCommand": "python manage.py collectstatic --noinput && gunicorn myproject.wsgi"
  }
}

# Manual deployment steps
python manage.py collectstatic --noinput
python manage.py migrate
gunicorn myproject.wsgi:application

🧠 Test Your Knowledge

What setting specifies where collectstatic gathers files?