Django mini-tip: the `path` lambda

January 17, 2010 at 10:26 PM | Python, Django | View Comments

There are a few things I do every time I start a new Django site, and one of those things is add my path lambda to the top of

    import os
    ROOT = os.path.abspath(os.path.dirname(__file__))
    path = lambda *args: os.path.join(ROOT, *args)

From then on, it's drop-dead simple to create absolute paths which are relative to the root of the project (or, at least, the directory which contains For example:

    >>> path('media/')

And some other places where it is useful:

    DATABASE_ENGINE = 'sqlite3'
    DATABASE_NAME = path('db.sqlite3')
    DATA_DIR = path('data/')