Skip to content

Q&A · Answered

How to serve Django static files in production

asked 9 months, 1 week ago 1417 views 1 answer
6

My static files work in development but not in production. How do I fix this?

1 Answer

One accepted
15

Accepted answer

Not confirmed since it was written, 9 months, 1 week ago
# Collect static files
python manage.py collectstatic
# settings.py
STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

# Use whitenoise for serving
# pip install whitenoise
MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware',
    ...
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
# Or use Nginx
location /static/ {
    alias /path/to/staticfiles/;
}

Your answer

Say what you tried, not only what worked — the next person needs the reasoning. Code goes in a fenced block.

Shown beside your answer and never checked, so it cannot be a name someone already has here.

No account needed to answer. Sign in instead and it carries your name and earns reputation.