[newbie] BASE_DIR vs. SETTINGS_DIR

138 views
Skip to first unread message

David

unread,
Mar 8, 2014, 4:09:38 PM3/8/14
to django...@googlegroups.com
Dear list,

I follow http://www.tangowithdjango.com/book/chapters/templates_static.html

The book instructs me to insert a SETTINGS_DIR and PROJECT_PATH
variables in my project settings.py.
However, the automatically created settings.py already contains a line

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

that looks very similar to the next three lines I added.

Am I reproducing code here? Do I make a mistake if I take the BASE_DIR~
line out?

Thanks for your guidance!

David


settings.py (slection):

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))

SETTINGS_DIR = os.path.dirname(__file__)
PROJECT_PATH = os.path.join(SETTINGS_DIR, os.pardir)
PROJECT_PATH = os.path.abspath(PROJECT_PATH)

Russell Keith-Magee

unread,
Mar 8, 2014, 7:10:55 PM3/8/14
to Django Users
On Sun, Mar 9, 2014 at 5:09 AM, David <ld...@gmx.net> wrote:
Dear list,

I follow http://www.tangowithdjango.com/book/chapters/templates_static.html

The book instructs me to insert a SETTINGS_DIR and PROJECT_PATH
variables in my project settings.py.
However, the automatically created settings.py already contains a line

BASE_DIR = os.path.dirname(os.path.dirname(__file__))

that looks very similar to the next three lines I added.

Am I reproducing code here? Do I make a mistake if I take the BASE_DIR~
line out?

Thanks for your guidance!

Ultimately, it won't really matter, as long as the final paths a complete and correct.

I'm not sure which version of Django "Tango with Django" was originally written with, but if it was written pre-1.6, then their advice was completely sound. Django 1.5 and earlier didn't have a BASE_DIR in the settings file, and it was very common to suggest users put a "project" directory in their settings file to make it easier to move code around.

Django 1.6 added BASE_DIR, giving the general pattern a standardised name.

As long as your own settings file is internally consistent, you'll be fine. 

If you want to have all three settings, you can do that; it will be microscopically less computationally efficient (since it will be computing the directory three times), but your settings files might be easier to read.

If you want to delete all references to BASE_DIR, that's fine, as long as you update all settings that reference BASE_DIR.

Alternatively, if you delete all references to SETTINGS_DIR and/or PROJECT_PATH, that's fine too - as long as you don't have any settings or code referring to them.

In the extreme case - you can delete all three, and replace them with hardcoded path strings.

If you're following the tutorial, it will probably be easier to stick with the SETTINGS_DIR/PROJECT_PATH pair, because the tutorial code will be referencing those settings. However, there's nothing magical about those two names; they're just shortcuts to make it easier to construct paths in your settings files.

Yours,
Russ Magee %-)

David

unread,
Mar 9, 2014, 8:13:27 AM3/9/14
to django...@googlegroups.com
Dear Russell,

thanks -- brilliant advice!

Greetings from Copenhagen,

David
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to django-users...@googlegroups.com
> <mailto:django-users...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.
> Visit this group at http://groups.google.com/group/django-users.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-users/CAJxq849mxgCRyNPDeUuCB-ghjd4f8zDO-AKFbEd%3DTJ_A7FL8fQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-users/CAJxq849mxgCRyNPDeUuCB-ghjd4f8zDO-AKFbEd%3DTJ_A7FL8fQ%40mail.gmail.com?utm_medium=email&utm_source=footer>.
> For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages