Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django paths, developing in windows, deploying on linux

Tags:

path

django

I'm developing Django apps on my local windows machine then deploying to a hosted linux server. The format for paths is different between the two and manually replacing before deployment is consuming more time than it should. I could code based on a variable in my settings file and if statements but I was wondering if anyone had best practices for this scenario.

like image 847
marr75 Avatar asked Mar 01 '10 21:03

marr75


1 Answers

The Django book suggests using os.path.join (and to use slashes instead of backslashes on Windows):

import os.path

TEMPLATE_DIRS = (
    os.path.join(os.path.dirname(__file__), 'templates').replace('\\','/'),
)

I think this is the best solution as you can easily create relative paths like that. If you have multiple relative paths, a helper function will shorten the code:

def fromRelativePath(*relativeComponents):
    return os.path.join(os.path.dirname(__file__), *relativeComponents).replace("\\","/")

If you need absolute paths, you should use an environment variable (with os.environ["MY_APP_PATH"]) in combination with os.path.join.

like image 74
AndiDog Avatar answered Oct 20 '22 03:10

AndiDog