Django settings.py mini-tip: the `path` lambda
January 17, 2010 at 10:26 PM | Python, Django | View CommentsThere 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 settings.py:
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 settings.py). For example:
>>> path('media/')
'/Users/wolever/code/review_anywhere/media/
And some other places where it is useful:
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = path('db.sqlite3')
...
TEMPLATE_DIRS = (
path('templates/'),
)
...
DATA_DIR = path('data/')
...