Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 513
Hafiz Avatar asked Oct 17 '25 08:10

Hafiz


2 Answers

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.

like image 102
Danica Avatar answered Oct 20 '25 19:10

Danica


Find the settings.py file in the project,

Put:

STATICFILES_DIRS=(os.path.join(BASE_DIR,'static'))

Change to:

STATICFILES_DIRS=[(os.path.join(BASE_DIR,'static'))]
like image 25
mubir Avatar answered Oct 20 '25 20:10

mubir



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!