Why use os.path.join():
Using os.path.join()
(and other os.path
functions) help to avoid setting hardcoded paths in your Django project. This way your project can be run on a different computer with a different directory structure without recoding everything. On your computer, the path may be "C:/ApathyBear/Django/mydjango" while on my computer it'll be "Users/Alex/django"
Django settings.py and the PROJECT_DIR
The way it is usually set in a Django project is through the PROJECT_DIR
setting. This will be something like PROJECT_DIR = os.path.dirname(os.path.dirname(__file__))
, which will take the current file (settings.py
), return the directory name, then the directory name that contains that directory. It will usually end up in your project root (may vary depending on your specific setup).
Once you have PROJECT_DIR
(or BASE_DIR
, or whatever you set it as), then it's easiest to set directories off of that. Using os.path.join()
combines the items you put it in. Thus, when you put in os.path.join(PROJECT_DIR, 'templates')
, as in your example, it will add templates
to your PROJECT_DIR. I'm guessing this is the value for the TEMPLATE_DIRS
setting, so your project now knows to look in "C:/ApathyBear/Django/mydjango/templates" for your computer or in "Users/Alex/django/templates" on my computer. Read the docs on os.path.join()
for more details on how it works, which is really useful when you don't know the OS you'll be running on.
Other questions
1) ON_OPENSHIFT is likely set earlier in the settings.py
file. It should be a boolean, thus either True
or False
. This setting is saying if ON_OPENSHIFT
is set to True
, then run these settings, otherwise run the other settings.
This is likely called earlier in the settings.py
file using an Environment Variable. Thus, on his Openshift machine, he probably has an environment variable "OPENSHIFT" set to True
. Then, in his settings.py
, he has something like: `ON_OPENSHIFT = os.environ.get('OPENSHIFT', 'False'). This is saying "Get the environment variable for OPENSHIFT. If there is none, then set it to False. Whatever the outcome of this is, set it equal to ON_OPENSHIFT.
2) The local version doesn't need to use an absolute path. That's likely a choice by your friend, though a somewhat odd one.
3) I'm guessing some code is missing. There should be something in the STATICFILES_DIRS
for when ON_OPENSHIFT is True. There's no magic; that setting won't work when ON_OPENSHIFT is True.