Search code examples
djangodjango-settingsdjango-staticfiles

path of STATICFILES_DIRS in django


I am from PHP background and want to give path to STATICFILES_DIRS in settings.py and in windows I understand that I will need to give full path like: D:/path/to/folder/project/static I want to know that is there a way to give some path like "/static/" or can we give path like dirname(__FILE__) in PHP?

So that my path is not hardcoded. Also why it is physical path as webserver normally don't follow physical path for loading CSS files as it use http path? So why it is this way(physical path) in django? Can some one please explain.

thanks for everyone's effort in advance.


Solution

  • In my settings.py files, I always do

    import os
    ROOT_PATH = os.path.dirname(__file__)
    

    Then you can do

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

    The reason that STATICFILES_DIRS wants a filesystem path is that it needs to know where the files live in the operating system (so things like manage.py collectstatic are possible). The STATIC_URL setting is for webserver paths, and that's what it uses to show to the user in the admin or the {% static %} tag or whatever. STATICFILES_DIRS is server-side only and never shows up in any rendered templates or anything.