Last Updated: February 25, 2016
·
4.642K
· rosatimatteo

Settings absolute paths in a Django project

Setting the correct path for MEDIA_ROOT and STATIC_ROOT is not a setting that comes out of the box when you start a new Django project.

To keep the same settings in multiple environments i always use this trick, so I can move the project around and always get the correct paths in my settings file.

At the top of your settings.py file, put the following lines:

import os
PROJECT_DIR = os.path.abspath(os.path.dirname(__file__))

Now when you need to define paths, just use the os.path.join function, like this:

MEDIA_ROOT = os.path.join(PROJECT_DIR, 'media')

and this:

STATIC_ROOT = os.path.join(PROJECT_DIR, 'static')

and place the 'static' and the 'media' directories in the same directory where settings.py is saved.