Introducing Livops 2.0! Promo codeCEOgives you 8,000 so‘m on your balance.What’s new

Django static files not loading when DEBUG = False: how to fix it

Django static files not loading when DEBUG = False, with CSS and JS answering 404 in production, is Django working as designed: runserver serves static files only while DEBUG is on. Set STATIC_ROOT, run collectstatic, and have something serve that folder: WhiteNoise inside the app, or a web server or CDN. Missing staticfiles manifest entry means a template asks for a file that is not in the manifest collectstatic wrote.

By Livops team, the people who build and run LivopsUpdated

The page comes back without styles, and the browser’s network tab shows 404 for every file under /static/. It happens the moment DEBUG goes to False, locally or on the first deploy. The admin looks just as bare.

Nothing is broken: Django stops serving static files when DEBUG is off, on purpose[1]. This page explains why, gives the production setup in three pieces, lists the causes when the setup is there and the files still do not load, including ValueError: Missing staticfiles manifest entry, and shows how to test it locally before you deploy. For the whole production setup of a Django app, see Django hosting.

Why it works with DEBUG on and not with DEBUG off

With django.contrib.staticfiles in INSTALLED_APPS, runserver serves static files for you when DEBUG is True[1]. Django calls that method grossly inefficient and probably insecure, unsuitable for production[1], so it is off whenever DEBUG is off. The same goes for the URL helpers people add to urls.py: static() works only in debug mode[1], and staticfiles_urlpatterns() only when DEBUG is True[2].

There is a switch, runserver --insecure, that serves static files even with DEBUG off. Django says it is only for local development, should never be used in production, and does not work with ManifestStaticFilesStorage[2].

What production needs

Three pieces: a folder, a command that fills it, and something that serves it.

1. STATIC_ROOT, the folder. The absolute path where collectstatic collects static files for deployment[3]. Django’s warning: it should be an initially empty destination folder, not a place to keep your static files permanently; those live in each app’s static/ folder and in STATICFILES_DIRS[3]. It must also differ from MEDIA_ROOT[3].

2. collectstatic, the command. python manage.py collectstatic copies all files from your static folders into STATIC_ROOT[1]; --noinput skips the prompt[2]. Run it whenever static files change, that is, on every deploy[4].

3. Something to serve STATIC_ROOT at STATIC_URL. Django’s deployment guide gives three ways: the web server in front of Django, a separate server for static files, or a cloud storage service or CDN[4]. On a platform the simplest is WhiteNoise, which serves the collected files from the Django app itself[8]:

# settings.py
from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent

STATIC_URL = "static/"
STATIC_ROOT = BASE_DIR / "staticfiles"

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",  # right after SecurityMiddleware
    # ... the rest
]

STORAGES = {
    "default": {"BACKEND": "django.core.files.storage.FileSystemStorage"},
    "staticfiles": {"BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage"},
}
pip install whitenoise
python manage.py collectstatic --noinput

WhiteNoise’s middleware goes directly after SecurityMiddleware and before all other middleware[8]. Its storage backend is optional: it compresses the files and gives them hashed names so they can be cached forever, on top of Django’s ManifestStaticFilesStorage[8]. Without it, WhiteNoise still serves the files: leave the STORAGES lines out[8].

Causes, most common first

1. Nothing serves STATIC_ROOT

collectstatic ran and the files are in STATIC_ROOT, but the app runs under Gunicorn with no WhiteNoise and nothing in front of it. Add WhiteNoise as above, or point your web server at STATIC_ROOT under STATIC_URL[4]. If WhiteNoise is in requirements.txt but not in MIDDLEWARE, or sits below other middleware, move it to right after SecurityMiddleware[8].

2. collectstatic never ran, or ran before STATIC_ROOT was set

Then STATIC_ROOT is empty or missing, and there is nothing to serve. Without STATIC_ROOT, collectstatic stops with[7]:

django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.

Set STATIC_ROOT and run collectstatic as part of every deploy, before the app starts. WhiteNoise looks for files only at startup, so files added after the app has started are not seen[8]: collecting while the app already runs does not help until it restarts.

3. STATIC_URL is wrong, or the templates hard-code paths

STATIC_URL must end in a slash[3]. A relative value such as static/ is prefixed with the server’s SCRIPT_NAME, or / if it is not set[3], so static/ gives /static/ and also works when the app is served under a sub-path. It must also differ from MEDIA_URL[3].

In templates, build each URL with the static tag rather than typing /static/...[1]:

{% load static %}
<link rel="stylesheet" href="{% static 'css/site.css' %}">

The tag goes through the configured storage, so it picks up a changed STATIC_URL and, with a manifest storage, the hashed file name[1][2].

4. STATICFILES_STORAGE on Django 5.1 or later

Django 4.2 added the STORAGES setting and deprecated STATICFILES_STORAGE in favour of STORAGES["staticfiles"][5]. Django 5.1 removed STATICFILES_STORAGE[6]. A settings file copied from an older tutorial that still sets it gets the default storage instead of the one it names, WhiteNoise’s included. Move it into STORAGES as in the block above.

Keep the default entry when you do: a STORAGES you define replaces Django’s default value and is not merged with it[3], so a dictionary with only staticfiles leaves uploaded files without a storage.

5. ValueError: Missing staticfiles manifest entry

ValueError: Missing staticfiles manifest entry for 'css/site.css'

This one is a 500, not a 404. ManifestStaticFilesStorage, and WhiteNoise’s storage built on it, writes the hashed name of every file to staticfiles.json once, when collectstatic runs[2]. With DEBUG off, a template that asks for a file not in that manifest raises this error[2][7]. Three usual causes:

  • The file does not exist where Django looks, often a typo in the static tag. python manage.py findstatic --verbosity 2 css/site.css shows every path Django searched[8].
  • collectstatic did not run for this version, so the manifest is from an earlier one, or there is none. Run it on every deploy.
  • Tests run with the manifest storage. Django advises another storage in tests, because collectstatic is not part of the normal test setup[2].

You can turn the error off: manifest_strict = False on a subclass of ManifestStaticFilesStorage[2], or WHITENOISE_MANIFEST_STRICT = False with WhiteNoise’s storage[8]. That hides a broken reference rather than fixing it.

A related error stops collectstatic itself: when a CSS file refers to a file that does not exist, the manifest storage cannot hash it and raises ValueError: The file '...' could not be found with ...[7]. Fix the path in the CSS, or add the missing file.

How to confirm the fix locally

Run with production settings, collected files and a production server, the way the platform will:

export DEBUG=0 ALLOWED_HOSTS=localhost
python manage.py collectstatic --noinput
gunicorn mysite.wsgi:application --bind 127.0.0.1:8000
curl -sI http://localhost:8000/static/admin/css/base.css | head -1

HTTP/1.1 200 OK means the static files are served without runserver; 404 means nothing serves them yet (cause 1 or 2). With a manifest storage, also open a few pages: a missing entry shows up as a 500 there, not on the CSS URL. This assumes your settings read DEBUG and ALLOWED_HOSTS from the environment and that django.contrib.admin is installed; otherwise use the path of one of your own files.

On Livops

  • collectstatic runs in the build. For a Django project, Livops runs python manage.py collectstatic --noinput during the build, if the project has django.contrib.staticfiles and STATIC_ROOT is set. Without STATIC_ROOT it does not run, and there is nothing to serve.
  • Serving is up to the app. With DEBUG = False Django serves no static files, so Livops’s docs point to WhiteNoise: pip install whitenoise, "whitenoise.middleware.WhiteNoiseMiddleware" right after SecurityMiddleware, and STATIC_ROOT = BASE_DIR / "staticfiles".
  • Do not collect at start. While the app runs, its code is read-only, so a start command that runs collectstatic cannot write into a STATIC_ROOT inside the project. The build is the place for it.
  • A failed build keeps the old version. If collectstatic stops on an error, such as a CSS file pointing at a file that is missing, the deploy fails as build_failed with the error in the build log, and the previous version keeps running.
  • DEBUG from Variables. Put DEBUG and SECRET_KEY in Variables, read them in settings.py, and deploy again after changing them.

What Livops does for Django: Frameworks. The other checks after the first deploy: DisallowedHost: Invalid HTTP_HOST header and CSRF verification failed.

Questions

Why do static files work with DEBUG = True and break with DEBUG = False?

With django.contrib.staticfiles installed and DEBUG on, runserver serves static files itself. Django calls that grossly inefficient and probably insecure, so it stops when DEBUG is off. In production something else has to serve them.

Can I just run runserver with --insecure?

Only on your own machine. Django's docs say --insecure is for local development and should never be used in production, and it does not work with ManifestStaticFilesStorage.

What is the difference between STATIC_ROOT and STATICFILES_DIRS?

STATICFILES_DIRS lists extra folders where your static files live permanently. STATIC_ROOT is an initially empty folder that collectstatic copies everything into for deployment. Never keep your own files in STATIC_ROOT.

Should I use STATICFILES_STORAGE or STORAGES?

STORAGES. Django 4.2 added it and deprecated STATICFILES_STORAGE, and Django 5.1 removed STATICFILES_STORAGE. Set the backend under STORAGES with the staticfiles key, and keep the default key too, because STORAGES replaces the default value instead of merging with it.

What does Missing staticfiles manifest entry mean?

The manifest storage could not find the file your template asked for in staticfiles.json. Either the file does not exist where Django looks, often a typo in the static tag, or collectstatic was not run for this version. python manage.py findstatic --verbosity 2 with the path shows where Django searched.

Does WhiteNoise need a CDN or Nginx in front?

No. WhiteNoise serves the collected files from your Django process. A CDN or a separate web server is another way to serve STATIC_ROOT, not a requirement.

Does Livops run collectstatic for me?

Yes, during the build, when the project has django.contrib.staticfiles and STATIC_ROOT is set. Serving the files is still up to the app, which is why Livops's docs point to WhiteNoise.

Sources

  1. Django docs — How to manage static files (docs.djangoproject.com). Checked .
  2. Django docs — The staticfiles app (docs.djangoproject.com). Checked .
  3. Django docs — Settings (docs.djangoproject.com). Checked .
  4. Django docs — How to deploy static files (docs.djangoproject.com). Checked .
  5. Django docs — Django 4.2 release notes (docs.djangoproject.com). Checked .
  6. Django docs — Django 5.1 release notes (docs.djangoproject.com). Checked .
  7. Django source — django/contrib/staticfiles/storage.py (6.1) (raw.githubusercontent.com). Checked .
  8. WhiteNoise — Using WhiteNoise with Django (whitenoise.readthedocs.io). Checked .

Put your project online today

Connect GitHub or drop a folder. No card is asked for.

Deploy your Django app

Got a question? — @livops_uz