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

5 Comments

  • 1
    Roger Lancefield
    July 29, 2008 - 11:38 pm | Permalink

    >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 🙂

  • 2
    atleta
    January 27, 2009 - 3:19 pm | Permalink

    >And it’s not only a problem for multideveloper projects, it’s a problem for almost every project as long as you develop on a different machine or just a different directory than where your app is (going to be) running. And it’s almost always the case.

    To make the code platform independent I use os.path.join instead of string formatting and I use the location of the Settings module to calculate the installation root instead of using the current directory (which may or may not work). I have the following lines at the beginning of my settings.py:

    def absp(path):
    os.path.join(ROOT,path)
    return os.path.join(ROOT,path)

    ROOT = os.path.dirname(os.path.realpath(__file__))

    then I use absp(“relative/path”) to set up the templates.

    My other trick is to have a module called devenv.py that I import in settings. That module checks whether it runs on a development environment and if so, it changes a few settings, e.g. the database one. Pretty important thing to do… To decide whether it’s the development or the live system I look at the path. If it contains /home/my_user_name, then it’s the devel directory.

    Mine is a single developer project, but this trick can be used for multideveloper too, if the settings to change are read from a config file which is either not version controlled centrally or has a uniq name for each developer. This is pretty standard for java project build setups. E.g. the local config can be called config.local or config.$username.

  • 3
    atleta
    January 27, 2009 - 3:22 pm | Permalink

    >I meant:
    def absp(path):
        return os.path.join(ROOT,path)

    ROOT = os.path.dirname(os.path.realpath(__file__))

  • 4
    August 16, 2014 - 7:07 pm | Permalink

    I see you share interesting things here, you can earn some additional money, your website has huge potential, for the
    monetizing method, just type in google – K2 advices how
    to monetize a website

  • 5
    July 21, 2017 - 4:07 pm | Permalink

    Hi admin, do you monetize your cottee.org ?
    There is easy method to earn decent money every month, just search on youtube
    : How to earn with wordai 4

  • Leave a Reply

    Your email address will not be published. Required fields are marked *