Relative Path for your Django Project

June 29th, 2010

djangoSometimes you need to move your Django project (example:from local computer to production server) or you work as a team which don’t have the same path to your project. If you give absolute path then you always need to change the settings.py.

The trick is to give relative path to your project.

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
)

While the sample settings said you need to add absolute path, I’d prefer to use relative path.

# settings.py
import os
PROJECT_PATH = os.path.realpath(os.path.dirname(__file__))
 
TEMPLATE_DIRS = (
    os.path.join(PROJECT_PATH, 'templates')
)

That’s it. Have fun!

Related posts:

  1. Django Tutorial – Simple Notes Application
  2. Using Django template in PHP
  3. Open Atrium, Open Source Project Management Software with Beauty in Mind

One Comment