Wednesday, 30 April 2008

Django Settings File

Whilst I love Django, there's a number of odd things about it which make me puzzled. One of those is the settings file. When you have a team of programmers it's important to be able to check each others code out with the minimum of fuss and to be able to deploy that code live in the same fashion.

Thus it was that the settings.py file used within Django has always puzzled me. You specify directories for where your templates go and you'll see the following


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

I have no idea why this does this, and I'm not the only person but as I'm as thick as a whale omelette I am not going to argue with the developers. The real 'wtf' is why the comments lead people to put paths in that will break when used on the next developer's machine. I'm developing on linux in my home directory, we deploy onto linux into a different users directory, the other developers mostly use Macs which have a different directory structure for homes.

Anyway - enough. As the ticket comments for this 'fault' say. The solution is simple. settings.py is a python file so do something like this.

import os
root_path=os.path.realpath(os.curdir)
TEMPLATE_DIRS = (
"%s/legacy/templates" % root_path,
"%s/edi/templates" % root_path,
)

And your world will be a better place.

1 comments:

Roger Lancefield said...

Heh, thanks Ian. I've recently started learning Django and when I first saw the templates config section in settings.py I made a mental note to find out how to avoid hard-coding paths. Looks like I need look no further :-)