Django in Production: Serving Static Files with Whitenoise

3D visualization of a Whitenoise turbine accelerating static file delivery from a Django server.

You deployed your Django app, but it looks terrible—all the CSS and images are missing! This is a common issue that can often be resolved by configuring Django Whitenoise to serve your static files correctly.

This is normal. Your dev server (runserver) is smart and serves static files for you. In production, your real server (like Gunicorn) doesn’t.

The easiest, most popular fix for this is Whitenoise.

How It Works

Whitenoise is a middleware that intercepts requests for static files and serves them directly, just like your dev server did. It’s built for production.

Step 1: Install Whitenoise

pip install whitenoise

Step 2: Edit settings.py

You need to tell Django to use Whitenoise. Find the MIDDLEWARE list in your settings.py.

# settings.py
MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware', # <-- Add this line
  'django.contrib.sessions.middleware.SessionMiddleware',
  # ... rest of middleware ...
]

The key is to place it right after SecurityMiddleware but before everything else.

Step 3: Run collectstatic

You need to gather all your static files (from all your apps) into one single folder that Whitenoise can find.

python manage.py collectstatic

This will create a new folder (usually staticfiles/) in your project.

Re-deploy your app. When the server restarts, Whitenoise will take over, and all your CSS and images will load correctly.

Similar Posts

Leave a Reply