Search code examples
djangodjango-settingsdjango-1.10

BASE_DIR returning settings path and not project path (django 1.10)


I am currently setting up my settings files for Django 1.10 as per Two Scoops For Django 1.8 preferred settings files set up.

my base.py settings file is:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))

the BASE_DIR is returning the following path:

/Devs/projects/captain_log/src/cap_log

my file tree:

|--Virtual Env Folder
    |--src(django project)/
       |--cap_log(django config)/
          |--init.py
          |--urls.py
          |--wsgi.py
          |--settings/
              |-- init.py
              |-- base.py (all settings located here)
              |-- development.py
              |-- production.py
              |-- etc.

I am under the assumption that the BASE_DIR is supposed to return:

/Devs/projects/captain_log/src/

I am asking because my STATIC_DIRS is also returning:

/Devs/projects/captain_log/src/cap_log/static

instead of:

 /Devs/projects/captain_log/src/static

Can someone please advise to a solution or correction to what I am doing. It is also effecting template paths, collectstatic, Media Path, etc.


Solution

  • Try one more dirname call

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

    The first dirname gives you settings, the second gives you the config folder, the third will put you in the parent directory

    __file__ # is the current file location
    os.path.abspath(__file__) # locates you on the file structure
    os.path.dirname(os.path.abspath(__file__)) # gives you the directory of the file at the supplied filepath
    

    The default assumption is you are using a settings.py file rather than a directory, so you are one directory shallow in the original config