If you’ve ever been happily styling your Django project and then suddenly lost all your CSS and JavaScript after setting DEBUG = False,
you’re not alone. This “disappearing static files” issue is one of the first surprises many Django developers run into when moving from development to production.
Let’s unpack why this happens, and how to fix it.
Why it works with DEBUG = True
When you’re building locally with DEBUG = True,
Django’s runserver is designed to make your life easier. It automatically serves your static files using the django.contrib.staticfiles app. You don’t need to configure anything special — your CSS, JavaScript, and images just show up.
This is fine for development. But Django’s philosophy is that in production you should use a dedicated web server or middleware for serving static files. Why? Because web servers like Nginx or Apache (or CDNs) are optimized for serving static assets efficiently, while Django is not.
What happens when DEBUG = False
Once you flip that setting, Django stops serving static files altogether. If you haven’t set up an alternative way to serve them, you’ll instantly notice that your site is broken — no styles, no images, no scripts.
This isn’t a bug; it’s by design. It’s Django’s way of reminding you:
“In production, static file handling is your responsibility.
How to fix it (the right way)
Here’s the proper workflow once you’re ready to run with DEBUG = False:
- Tell Django where to collect your static files In your settings.py, add:
STATIC_ROOT = BASE_DIR / "staticfiles"
- Run the collectstatic command
This command gathers all static files from your apps and places them into the
STATIC_ROOT
`folder.
python manage.py collectstatic
- Serve those files with a proper solution
Web server option: Configure Nginx or Apache to serve the contents of your STATIC_ROOT.
Middleware option: Use Whitenoise, which allows Django to serve static files in production without an external server. This is a common approach for simpler deployments (like on Heroku).
Key takeaway
Django makes static files easy to work with during development, but it hands over the responsibility once you go to production. That’s why your static files vanish when you set DEBUG = False
.
Understanding this behavior early on saves hours of confusion and makes your deployment process much smoother.
So next time you see your CSS disappear, don’t panic, just remember: Django doesn’t serve static files in production; you do.
Top comments (0)