Home > Enterprise >  Why Django 4.0 can access static files only with STATICFILES_DIRS, not STATIC_ROOT
Why Django 4.0 can access static files only with STATICFILES_DIRS, not STATIC_ROOT

Time:11-22

When I use STATIC_ROOT:

STATIC_ROOT = BASE_DIR / 'static'

Django cannot access static files and always return 404 error for them.

But when I use STATICFILES_DIRS:

STATICFILES_DIRS = [
    BASE_DIR / "static",
]

Everything works well. What's the issue?

I don't understand why Django works in this way, I always thought that STATIC_ROOT is correct way of creating route.

CodePudding user response:

STATIC_ROOT is where the collectstatic command will copy all the static files in.

This folder is meant for production use. So that all the static files in your project (from your apps, or third party apps etc.) are kept in a single folder and therefore becomes easy to serve in production.


By default, Django automatically searches for static files inside each app directory (this is helpful in writing reusable apps). The STATICFILES_DIRS setting allows you to define extra places to look for the static files.

Also, the STATIC_ROOT folder and the STATICFILES_DIRS folder should not be the same values.

  • Related