0
0
DjangoHow-ToBeginner · 3 min read

How to Serve Static Files in Production in Django

In production, Django does not serve static files by default. Use WhiteNoise middleware or configure your web server (like Nginx) to serve static files from the STATIC_ROOT directory after running python manage.py collectstatic.
📐

Syntax

To serve static files in production, you need to collect all static files into one folder and serve them using a web server or middleware.

  • STATIC_ROOT: The folder where static files are collected.
  • python manage.py collectstatic: Command to gather static files into STATIC_ROOT.
  • WhiteNoise: Middleware to serve static files directly from Django.
  • Alternatively, configure your web server (e.g., Nginx) to serve files from STATIC_ROOT.
python
STATIC_ROOT = '/path/to/staticfiles/'

# Run this command in terminal:
python manage.py collectstatic

# Add WhiteNoise middleware in settings.py
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # other middleware
]

# Serve static files with WhiteNoise
STATIC_URL = '/static/'
💻

Example

This example shows how to configure Django to serve static files in production using WhiteNoise middleware.

python
# settings.py
import os

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    # other middleware
]

# Run this in terminal to collect static files:
# python manage.py collectstatic

# Then run your production server normally

# Terminal commands:
# python manage.py collectstatic
# gunicorn myproject.wsgi:application
Output
Static files copied to /path/to/project/staticfiles Starting production server with static files served by WhiteNoise
⚠️

Common Pitfalls

  • Not running collectstatic before deploying causes missing static files.
  • Forgetting to set STATIC_ROOT means files are not collected.
  • Not adding WhiteNoise middleware or web server config means static files won’t be served.
  • Using Django’s development server (runserver) in production is not recommended for static files.
python
# Wrong: Missing collectstatic
# STATIC_ROOT not set

# Right:
import os
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Then run:
# python manage.py collectstatic

# Wrong: Missing WhiteNoise middleware
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    # 'whitenoise.middleware.WhiteNoiseMiddleware',  # Missing
]

# Right:
MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
]
📊

Quick Reference

  • Set STATIC_ROOT to a folder for collected static files.
  • Run python manage.py collectstatic before deployment.
  • Add WhiteNoiseMiddleware to MIDDLEWARE for simple static serving.
  • Or configure your web server (Nginx/Apache) to serve STATIC_ROOT at /static/.
  • Never use Django's runserver for production static files.

Key Takeaways

Always set STATIC_ROOT and run collectstatic before deploying to production.
Use WhiteNoise middleware or a web server to serve static files in production.
Django’s development server does not serve static files in production.
Configure your web server to serve static files from STATIC_ROOT for best performance.
Missing middleware or collectstatic causes static files to not appear in production.