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.

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.


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.


Find the *settings.py8 file,

Put:

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

Change to:

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

Need Your Help

how to Send & symbol in xml body to server

php html asp.net ios iphone

I am trying to send the & symbol to the server in an XML body but only received a blank.

How hard is it to migrate a web app from localhost to a hosting platform?

deployment

Since I'm not a huge fan of any of the current solutions for managing the resources and knowledge that I have, I was thinking about making my own solution, which will involve custom code as well as