How To Use Relative Paths In Django Settings

Do you like this?

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 every time you change the location of your app or simply changing and uploading the file via FTP from your testing environment to your server.


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 file.

import os.path

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

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

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

comments powered by Disqus