Gilles Vanermen
Gilles Vanermen

Reputation: 65

Django: BASE_DIR not correct after the os.path.join(...) command

I am getting the following behavior in Django:

BASE_DIR seems to change when I use the "os.path.join(...)" command on it.

My settings.py file:

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

In the Python Shell:

>>> import os
>>> from django.conf import settings

>>> base_dir = settings.BASE_DIR
***'C:\\Users\\gille\\timeless_wisdom'***

>>> file_path = os.path.join(base_dir, '/timeless_wisdom/UserData')
***'C:/timeless_wisdom/UserData'***

So: when I join a relative path with BASE_DIR, I don't get the expected result, but he starts from C:/ again...

I have tried the following, but same result.:

file_path = os.path.join(base_dir, '\\timeless_wisdom\\UserData')

I have tried using PROJECT_ROOT instead of BASE_DIR, but same result.

Anything I am missing? Thanks...

Upvotes: 2

Views: 8438

Answers (2)

Fahad Ali
Fahad Ali

Reputation: 1

setting.py

SESSION_ENGINE = 'django.contrib.sessions.backends.file'
SESSION_FILE_PATH = BASE_DIR / 'session'

BASE_DIR is another method to add external file to the django inner project - setting.py

The above code store the session data in file which is created in root project file.

FILE_PATH = BASE_DIR / 'Foldername'

Upvotes: -1

Daniel Roseman
Daniel Roseman

Reputation: 599778

You shouldn't have any leading slashes on the path.

file_path = os.path.join(base_dir, 'timeless_wisdom/UserData')

Upvotes: 4

Related Questions