2

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.

aNoNyMoUs
  • 61
  • 7

2 Answers2

3

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/.

dirkgroten
  • 20,112
  • 2
  • 29
  • 42
suhailvs
  • 20,182
  • 14
  • 100
  • 98
0

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

shoytov
  • 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