Different settings.py files for server and development

1 view
Skip to first unread message

Dana

unread,
Oct 23, 2008, 5:55:55 PM10/23/08
to Django users
Hello everyone,

I know a form of this question has been asked before on this group but
I can't for the life of me find the thread.

I am wondering how I can have a generic settings.py file that contains
all my basic settings and then have a settings-local.py (or
whatever...) and have that contain custom settings such as DEBUG=True,
etc... Ideally the settings-local.py file would *only* have the custom
settings, and nothing else, but I cannot seem to get this to work. For
example:

In settings.py I would have default settings:

settings.py
-------------------------------------------
DEBUG = False

DATABASE_ENGINE = 'mysql'

DATABASE_NAME = 'something'

DATABASE_USER = 'root'
DATABASE_PASSWORD = ''

MEDIA_ROOT = '/home/user/media/'
MEDIA_URL = 'http://media.example.com/'

ADMIN_MEDIA_PREFIX = '/admin_media/'

INSTALLED_APPS = (
....
)

...... etc

-------------------------------------------
and in settings-local.py I would override the settings:

# settings-local.py
-------------------------------------------
DEBUG = True

DATABASE_USER = 'username'
DATABASE_PASSWORD = 'somethinghere123'
-------------------------------------------

I would like some way to have settings-local import my settings.py
file and then override specific settings. Anyone know how to do this
cleanly?

Thanks!

felix

unread,
Oct 23, 2008, 6:54:25 PM10/23/08
to django...@googlegroups.com

at the very bottom of settings.py :

from local_settings import *


this overwrites just the imported keys/values

-flx

Dana

unread,
Oct 23, 2008, 6:59:35 PM10/23/08
to Django users
Sweet, I knew it was simple :)

Thanks!

On Oct 23, 3:54 pm, felix <crucialfe...@gmail.com> wrote:
> at the very bottom of settings.py :
>
> from local_settings import *
>
> this overwrites just the imported keys/values
>
> -flx
>

Ned Batchelder

unread,
Oct 23, 2008, 8:04:44 PM10/23/08
to django...@googlegroups.com
In settings.py:

try:
from settings_local import *
except:
pass

--Ned.
http://nedbatchelder.com

--
Ned Batchelder, http://nedbatchelder.com


Dana

unread,
Oct 23, 2008, 8:18:37 PM10/23/08
to Django users
Ned/Felix,

Ok, I tried your guys solution but it does not seem to be working for
me, so Im wondering where I goofed.

Here is what ive got

My settings.py and settings-custom.py file are here:

config/
settings.py
settings-custom.py

The directory containing config is on the PythonPath, so an import of:

from config.settings-custom import *

... should work correct?

In settings.py I have DEBUG=False and in settings-custom.py I have
DEBUG=True, but Im getting my 500.html page, so that means it is only
respecting the DEBUG setting in settings.py.

Any ideas?

Thanks

On Oct 23, 5:04 pm, Ned Batchelder <n...@nedbatchelder.com> wrote:
> In settings.py:
>
>     try:
>        from settings_local import *
>     except:
>        pass
>
> --Ned.http://nedbatchelder.com

Dana

unread,
Oct 23, 2008, 8:20:39 PM10/23/08
to Django users
Never mind, answered my own problem. I had a missing setting that was
causing a 500 error before it could access the settings-custom.py.

Your solutions worked!

Thanks!

Ned Batchelder

unread,
Oct 23, 2008, 10:07:00 PM10/23/08
to django...@googlegroups.com
settings-custom isn't a valid Python file name, because a Python identifier can't have a dash in it.  Use an underscore.


--Ned.
http://nedbatchelder.com

Dana wrote:

Ned Batchelder

unread,
Oct 23, 2008, 10:08:51 PM10/23/08
to django...@googlegroups.com
And BTW, my bad.  Change the except: line to except ImportError, then the syntax error from the dash will be apparent in your 500 page.

--Ned.
http://nedbatchelder.com

D. Woodman

unread,
Oct 23, 2008, 10:50:11 PM10/23/08
to django...@googlegroups.com
Thanks Ned!

Cheers

Ross Dakin

unread,
Oct 24, 2008, 6:44:40 AM10/24/08
to Django users
I do it like this:


# settings.py
[ ... ]
try:
from settings_dev import *
except ImportError:
pass


# settings_dev.py
import os

DEBUG = True
DATABASE_ENGINE = 'sqlite3'
DATABASE_NAME = 'dev.db'
MEDIA_ROOT = os.path.abspath('../') + '/public/media/'
MEDIA_URL = 'http://127.0.0.1:8000/media/'
TEMPLATE_DIRS = (os.path.abspath('') + '/templates')

Doug Harris

unread,
Nov 11, 2008, 5:11:04 PM11/11/08
to Django users
One thing I've noticed is that some settings don't stick if I override
them in local_settings.py.

One key example is CACHE_BACKEND. Has anybody seen this sort of
behavior?


On Oct 23, 5:54 pm, felix <crucialfe...@gmail.com> wrote:
> at the very bottom of settings.py :
>
> from local_settings import *
>
> this overwrites just the imported keys/values
>
> -flx
>
Reply all
Reply to author
Forward
0 new messages