4

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.

7 Answers 7

24

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So if STATICFILES_DIRS path is not correct then STATIC_URL will also not be able to find the static folder on server?..
@Hafiz Not exactly. If STATIC_URL is /static and you have a template with <img src="{{ STATIC_URL }}/images/foo.png" />, then that'll render just fine (assuming your webserver is set up properly to serve things out of the static directory) -- that has nothing to do with STATICFILES_DIRS (or Django, for that matter). But if that's not set properly, then Django-side things that need to be able to find the actual files (e.g. manage.py collectstatic or the manage.py runserver dev server serving your static files) won't work.
This is wrong. I've got the next error "ImproperlyConfigured: Your STATICFILES_DIRS setting is not a tuple or list; perhaps you forgot a trailing comma?"
@Rusfearuth Sorry; you could just add brackets around what I said (now edited). Your answer does something different.
4

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'))] 

Comments

2

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.

2 Comments

I will look into your solution as well, however, I marked correct above one because that worked for me
Not working for me, I have: STATICFILES_DIRS = ( os.path.join(BASE_DIR, '..', 'static'),) . At the same time, STATIC_ROOT is commented and not used. So now my dev static files are working. If I use "collectstatic" management command, I have to comment STATICFILES_DIRS and not comment STATIC_ROOT. It is rare.
1

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') ] 

Comments

1

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? 

Comments

0

your syntax is correct, I think you forget of importing os at the first line of code as import os.

2 Comments

Thanks for your time and help, could you consider giving your answer with correct syntax example? That could help others to get a quick solution from your post.
Yes, you've to import os in the beginning. I did not mention it as I thought it was obvious. Also when you create a django project, the import line is already present in settings.py
0

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',] 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.