'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 1:[1]
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.
Solution 2:[2]
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'))]
Solution 3:[3]
Answer on your question is wrong. If you do like this, you'll get the next error:
ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?
My decision is add to top of settings.py file the next function:
import os def look_folder_tree(root): result = () for dir_name, sub_dirs, file_names in os.walk(root): for sub_dir_name in sub_dirs: result += (os.path.join(dir_name, sub_dir_name),) return result # Django settings for project. PROJECT_DIR = os.path.dirname(__file__)
and call look_folder_tree function at:
# Absolute path to the directory static files should be collected to.
# Don't put anything in this directory yourself; store your static files
# in apps' "static/" subdirectories and in STATICFILES_DIRS.
# Example: "/var/www/example.com/static/"
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')
# URL prefix for static files.
# Example: "http://example.com/static/", "http://static.example.com/"
STATIC_URL = '/static/'
# Additional locations of static files
STATICFILES_DIRS = look_folder_tree(STATIC_ROOT)
My decision is allow to add all sub folders inside static directory.
Solution 4:[4]
In settings.py
:
Import os (already done when you create a django project)
import os
You can just use STATICFILES_DIRS as a tuple, mind the trailing comma.
STATICFILES_DIRS = (
os.path.join(BASE_DIR, 'static'),
)
Or you can use the STATICFILES_DIRS as a list data-type, comma at the end is not required here:
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static')
]
Solution 5:[5]
Change this:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'))
To This:
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
Add a comma at last as,
ERRORS:
?: (staticfiles.E001) The STATICFILES_DIRS setting is not a tuple or list.
HINT: Perhaps you forgot a trailing comma?
Solution 6:[6]
your syntax is correct, I think you forget of importing os at the first line of code as import os.
Solution 7:[7]
If you are using newer versions of Django i.e Django 3.1, and your BASE_DIR
is like this ...
BASE_DIR = Path(__file__).resolve().parent.parent
Then do this :-
STATICFILES_DIRS =[BASE_DIR / 'my_app/static',]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Arindam |
Solution 3 | |
Solution 4 | |
Solution 5 | Mohnish |
Solution 6 | Hailemariam Moges Teshager |
Solution 7 | cursorrux |