Can anyone explain to me the need of providing STATIC_ROOT in a django project? As it seems, Everything works fine if I just declare the STATICFILES_DIRS and not STATIC_ROOT.
-
see https://stackoverflow.com/a/18628930/ – suhailvs Apr 02 '20 at 08:53
-
Note if you just put your static files inside _static/_ directories inside your apps, you don't even need to define STATICFILES_DIRS – dirkgroten Apr 02 '20 at 09:11
-
Then why are we even doing these if we don't need them in our project? – aNoNyMoUs Apr 02 '20 at 09:22
-
@aNoNyMoUs there is cases that static files folder outside the django project folder, then only you need to define `STATICFILES_DIRS`. – suhailvs Apr 02 '20 at 10:33
2 Answers
STATIC_ROOT
is only needed in production, since the Django development server runserver
takes care of serving static files directly from your static directories.
STATIC_ROOT
is the absolute path to the directory where python manage.py collectstatic
will collect static files for deployment.
Example: STATIC_ROOT="/var/www/example.com/static/"
Now the command python manage.py collectstatic
will copy all the static files(ie in static folder in your apps, static files in all paths) to the directory /var/www/example.com/static/
. now you only need to serve this directory on apache or nginx..etc.
Note: You often see examples where STATIC_ROOT
is set to os.path.join(BASE_DIR, 'static')
. This is a bad practice as it creates the static folder inside your source code repository. Always set it to a path outside of your project folder, e.g. /var/www/example.com/static/
when your project is in /var/www/example.com/src/
.

- 20,112
- 2
- 29
- 42

- 20,182
- 14
- 100
- 98
when you change DEBUG in settings.py to False on your production server, if you not using STATIC_ROOT, the files on the page will not be shown. And yes, you must do collectstatic
as it was written in the previous answer

- 585
- 4
- 10
-
Also, when we refer static files in our html, are we referring the file from the dir that got created from doing createstatic or the source dir which was used in createstatic? – aNoNyMoUs Apr 02 '20 at 09:29