STATIC_ROOT vs STATICFILES_DIRS

Hello,

I set the STATIC_ROOT as:

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

And it doesn’t work.

Then I remove it and set STATICFILES_DIRS as:

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]

And it works.

So, what is STATIC_ROOT then?

Covered in the docs:

STATIC_URL = '/static/'

if not DEBUG:
    STATIC_ROOT = '/home/django/www-data/example.com/static/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static/'),
]

I caution people against blindly believing what they read on SO. In this particular example, the most up-voted answer makes this blanket statement about the STATIC_ROOT setting:

STATIC_ROOT is useless during development, it’s only required for deployment.

This is a dangerously-wrong assumption, and ignores those environments where development is done in an environment that attempts to mimic a production environment, or those situations where production environments are automatically built from the development environment.

It also goes on to say:

You don’t even need to set it. Django looks for static files inside each app’s directory (myProject/appName/static ) and serves them automatically.

This is also wrong by saying that Django “serves them automatically.” These files are only served by runserver if it is configured to do so with the appropriate settings.

These requirements and the functionality provided by them are well documented in the official docs.

This is yet another example of why I caution people to not solely rely upon SO, and to not trust the answers found there without verifying what they have found through other resources.

1 Like