Django Static Files 404 in Production (DEBUG False): Fix
CSS and admin styles vanished after you set DEBUG to False? Set STATIC_ROOT, run collectstatic on every deploy, and serve the files with WhiteNoise or nginx pointed at the right folder.
Django static files return 404 in production because Django only serves them itself when DEBUG = True. With DEBUG = False, you must run collectstatic to copy every file into STATIC_ROOT, then serve that folder with WhiteNoise or a web server such as nginx. The most common mistakes are a missing STATIC_ROOT, skipping collectstatic in the deploy, and nginx pointing at the wrong folder.
The symptom is easy to recognise. The site loads, but the page has no styling, the admin looks like plain HTML, and the browser console shows 404 for /static/... files. It often appears right after you turn DEBUG off for the first time. This guide walks through the checks in order so you can fix it in a few minutes and stop it coming back.
Step 1: Confirm It Really Is a Static File 404
Open the browser developer tools, go to the Network tab and reload. Click one of the red /static/ requests.
- 404: the file is not where the server looks for it (Steps 2 to 5).
- 500: your app crashed while building the URL, often a WhiteNoise manifest problem (Step 4).
- 403: the web server cannot read the folder (Step 5).
- 200 but wrong content (HTML instead of CSS): a catch-all route or proxy is answering instead. The browser reports this as a MIME type error. We cover the same pattern for Vite in Vite Blank Page After Deploy: MIME Type text/html Error.
Also open the URL in a new tab. If you see Django's own 404 page, the request reached Django, which means nothing is serving static files in front of it.
Step 2: Set STATIC_URL and STATIC_ROOT
Two settings control static files. STATIC_URL is the public path. STATIC_ROOT is the single folder where collectstatic gathers everything for production.
# settings.py
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_URL = "/static/"
STATIC_ROOT = BASE_DIR / "staticfiles"
# Optional: your own project-level static folder
STATICFILES_DIRS = [BASE_DIR / "static"]
Do not put the same folder in both STATIC_ROOT and STATICFILES_DIRS. Django refuses that, and for good reason: collectstatic would copy a folder into itself. Think of STATICFILES_DIRS as the input and STATIC_ROOT as the output. A missing STATIC_ROOT is the first thing to check when collectstatic raises ImproperlyConfigured.
Step 3: Run collectstatic on Every Deploy
Your app's CSS lives in many places: your own apps, the Django admin, and third-party packages. collectstatic copies all of it into STATIC_ROOT.
python manage.py collectstatic --noinput
Check the result:
ls staticfiles/admin/css | head
If the folder is empty or missing, the files were never collected. Add the command to your deploy script, your Dockerfile or your platform build step. A common trap is collecting files once by hand on the server and then deploying from a fresh container. The new container has no staticfiles folder, so every file 404s again.
A Dockerfile example:
RUN python manage.py collectstatic --noinput
CMD ["gunicorn", "config.wsgi:application", "--bind", "0.0.0.0:8000"]
If collectstatic needs environment variables such as SECRET_KEY at build time, pass a dummy value for the build step only. Never bake your real secrets into the image.
Step 4: Serve the Files With WhiteNoise
Gunicorn does not serve static files. For a small team, the simplest answer is WhiteNoise, which lets your Django app serve its own static files with no nginx setup. Install it:
pip install whitenoise
Add the middleware directly after SecurityMiddleware, and choose a storage backend:
# settings.py
MIDDLEWARE = [
"django.middleware.security.SecurityMiddleware",
"whitenoise.middleware.WhiteNoiseMiddleware",
# ...the rest of your middleware
]
STORAGES = {
"default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
"staticfiles": {
"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
},
}
The STORAGES setting replaced STATICFILES_STORAGE in Django 4.2. On older versions, use the old setting name.
The manifest storage adds a hash to each file name, so browsers cache files safely and fetch new ones after a deploy. It also has a sharp edge. If a template or a CSS file points to a file that does not exist, Django raises ValueError: Missing staticfiles manifest entry and the page returns a 500 instead of a 404. Forum reports describe this for the admin when the collected files are stale, for example in this Django forum thread about admin styles and WhiteNoise. The fix is to rerun collectstatic after clearing the old folder, then redeploy.
Step 5: If You Use nginx, Point It at STATIC_ROOT
Some setups serve static files from nginx in front of Gunicorn. The usual mistake is pointing alias at the wrong folder. Community answers repeat the same rule: nginx must point at STATIC_ROOT, not at STATICFILES_DIRS (see this thread on nginx and Django static files).
location /static/ {
alias /srv/app/staticfiles/;
}
Check three details:
- The trailing slash. If
locationends in/, thealiaspath should too. - Permissions. The nginx user needs read access to every folder in the path. A 403 usually means it does not have it.
- Leftover rules. An old
location /static/admin/block that points at a system copy of Django can serve outdated admin files. Remove it, becausecollectstaticalready gathers the admin files.
Reload nginx with sudo nginx -t && sudo systemctl reload nginx.
Step 6: Check Media Files Separately
User uploads are not static files. They go in MEDIA_ROOT and are served from MEDIA_URL. collectstatic ignores them, and neither Django nor WhiteNoise serves uploads in production. Use object storage such as S3 or Cloudflare R2, or an nginx location /media/ block. If your logo works but uploaded avatars 404, this is the cause.
Step 7: Catch It Before Users Do
A static file 404 does not crash your app. Health checks still return 200, and an uptime ping on / still passes, because the HTML loads fine. Users just see a broken page. Add a post-deploy check that requests one known file, such as /static/admin/css/base.css, and fails the deploy if it does not return 200. This is the same gap we describe in Uptime Monitor Says Up but Users See Errors. If a bad ALLOWED_HOSTS is also causing trouble after a deploy, see Django DisallowedHost: Invalid HTTP_HOST Header.
How Relia Helps
A broken stylesheet is easy to miss because the server reports no error. Relia has a Python package for Django apps. It watches users, requests and errors in a live product, and it also supports uptime monitors and checks of key pages. If a deploy leaves a key page failing, Relia joins the deploy and the failing requests into one incident and explains it in plain words. You can try the live demo first.
FAQ
Why does Django not serve static files when DEBUG is False?
The development server serves them only for convenience. Production servers are expected to serve static files from a dedicated folder, which is why collectstatic and STATIC_ROOT exist.
Do I need nginx to serve Django static files?
No. WhiteNoise serves them from the Django process, and it works well for small and medium apps. Use nginx or a CDN when traffic is high or you want to offload the files completely.
Why is the Django admin unstyled in production?
The admin CSS is a static file like any other. Run collectstatic, make sure WhiteNoise or nginx serves STATIC_ROOT, and check that no old nginx rule points at a different admin folder.
Should I commit the staticfiles folder to git?
No. It is build output. Generate it in your deploy step and add it to .gitignore.