djangodjango-templatesdjango-staticfiles

Django STATIC_URL value "/static/" vs "static/"?


I'm a junior dev and know how pathing works. I'm wondering what the paths for the directory that static files will be served from are if the values for are STATIC_URL="/static/" and STATIC_URL="static/". I know there is a different since the latter cause my app to not find the static files even though it's the default value. Thank you!


Solution

  • If STATIC_URL = '/static/', Django will serve the static files from the /static/ directory at the root of your domain. For example, if your domain is www.example.com, Django will look for static files at www.example.com/static/.

    On the other hand, if STATIC_URL = 'static/', Django will serve the static files from the static/ directory relative to the current URL. This means that if you're on the page www.example.com/blog/, Django will look for static files at www.example.com/blog/static/.

    The leading slash in the STATIC_URL setting is very important. It determines whether the path is absolute (starts from the root) or relative (starts from the current URL).