How To Use Relative Paths In Django Settings

Do you like this?

Summary:
I have been using this technique for a while and it seems to be very handy especially in the production environment. You don't have to change the paths in your Django settings.py every time you change the location of your app or simply changing and uploading the settings.py file via FTP from your testing environment to your server.


Content:

One of the most useful features of this trick is to set relative paths for the TEMPLATE_DIRS variable but it can also be used for other variables such as STATICFILES_DIRS or any other custom variables you want to define in your settings.py file.

import os.path

#this will give the current location of your settings.py file
SITE_ROOT = os.path.dirname(os.path.realpath(__file__))

STATICFILES_DIRS = (
    os.path.join(SITE_ROOT, 'resources'),
)

TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'html'),
)

 
comments powered by Disqus