'What is the absolute path of BASE DIR?

Django newbie here. I have trouble understanding the meaning of:

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

and

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

What's happening here?

I take it the "file" is the settings.py file we are in (?), so the BASE_Dir is two folders up from that...? i.e. the one with manage.py in it?

So the STATIC_ROOT, will be one? or two? directories up from the BASE_DIR. Will the STATIC_ROOT FOLDER be made for me? Or do I have to make one called "static"?

└── MY_PROJECT
    ├── BASE_DIR
    │   ├── MY_APP
    │   │   └── settings.py
    │   └── manage.py
    └── static

Is the above right for this example? Then what the heck / where the heck will the STATIC_FILES_DIRS be?



Solution 1:[1]

BASE_DIR is your Django project directory. The same directory where manage.py is located.

Solution 2:[2]

If your settings.py is configured like this, your filesystem looks like this:

??? MY_PROJECT
    ??? BASE_DIR
    ?   ??? MY_APP
    ?   ?   ??? settings.py
    ?   ??? manage.py
    ??? static        -> STATIC_ROOT
        ??? static    -> STATICFILES_DIRS

But it is not a good configuration because it mixes up collected statics and the directory where Django tries to find static files (e. g. to collect them). May be better to use this:

??? MY_PROJECT
    ??? BASE_DIR
        ??? my_app
        ?   ??? settings.py
        ?   ??? static              -> STATICFILES_DIRS
        ??? manage.py
        ??? deployment
            ??? collected_static    -> STATIC_ROOT
            ??? media               -> MEDIA_ROOT

# settings.py
BASE_DIR = os.path.dirname(os.path.dirname(__file__))
STATICFILES_DIRS = (os.path.join(
    BASE_DIR, "my_app", "static"),)
STATIC_ROOT = os.path.join(
    os.path.dirname(BASE_DIR), "deployment", "collected_static")
MEDIA_ROOT = os.path.join(
    os.path.dirname(BASE_DIR), "deployment", "media")

Now you can easily deploy your static and media files using your favorite webserver (Apache, Nginx etc.) pointing it to the "deployment" directory.

Update:

I added also a recommendable configuration for MEDIA_ROOT and changed the path for the collected static.

Solution 3:[3]

This answer was written for Python 3 and Django 3.x.

It is common to set up a Django project for dev, prod and perhaps test settings. These settings usually inherit from base settings, which are defined in a file called your_app_name/settings/base.py. Within base.py, BASE_DIR might be defined like this:

BASE_DIR = Path(__file__).resolve().parent.parent.parent

The manage.py shell subcommand accepts a line to be interpreted by using the -c option. Here is a one-line solution that displays BASE_DIR for a Django app called frobshop:

$ ./manage.py shell -c 'from frobshop.settings import base; print(f"BASE_DIR={base.BASE_DIR}")'
BASE_DIR=/var/work/django/frobshop

I provide additional information in my Django Notes blog page.

Solution 4:[4]

for path settings in django projects i use

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_PRODUCTION_DIR = os.path.abspath(os.path.join(
os.path.dirname(__file__), '..', '..', 'static_production'))

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(STATIC_PRODUCTION_DIR, "static")

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(STATIC_PRODUCTION_DIR, "media")


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

Solution 5:[5]

What I did and that really helped me was :

  1. go to settings.py and add --> STATICFILES_DIRS = [os.path.join(BASE_DIR, 'static')] at the end of file.

  2. Go to the base html template in your templates folder, and add these 2 lines in the head section (Just make sure you create a folder in static. in my case it is css as you can see below, could be different name ):

    a){% load static %} b)

Solution 6:[6]

if anything contains path in it that you didn't understand where is it or how it works just type in the bottom of your file print('my directory', UNKNOWN_DIR) then run in Terminal Python manage.py runserver.

'my directory' is just a string you can type in any string then UNKNOWN_DIR is what you wanna know where is it. then run Python manage.py runserver.

EXEMPLES:

print('my path', (os.path.abspath(file)) )

python manage.py runserver

print('my path', (os.path.dirname(os.path.abspath(file))) )

python manage.py runserver

print('my path', os.path.join(BASE_DIR, 'staticfiles') )

python manage.py runserver

Solution 7:[7]

Add this code below to "settings.py" so that you can see the absolute path of "BASE_DIR" on your console:

# "settings.py"

print("BASE_DIR is", BASE_DIR)

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 Jon
Solution 2
Solution 3
Solution 4 Dimitris Kougioumtzis
Solution 5 Community
Solution 6
Solution 7 Kai - Kazuya Ito