I like to keep all local settings files in my versioning repository. The way I differentiate between them is by querying the hostname of the local machine. I've got a host_settings folder with local settings files. Notice that the local settings file names go by a convention where '.' is replaced with underscores.
Example: host_settings/local_machine_tld.py
1 2 3 4 5 6 | try:
import socket
hostname = socket.gethostname().replace('.','_')
exec "from host_settings.%s import *" % hostname
except ImportError, e:
raise e
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Very useful indeed. I use a similar approach:
settings.py:
I have a question though. Somebody told me this may not work in Mac or Windows. Have you tried that?
#
I haven't tried it out on Windows but OSX correctly returns the hostname.
#
Wow, what a great idea!
More code, shorter filenames:
#
Use of "exec" will scare some users away from this snippet. There may be valid use cases for exec, but many Python programmers IMHO avoid the use of it like the plague. Kind of like a goto statement in a C program - it's just not done :)
Using the built-in _import_ might be the way to go.
#
I tried to do something with import but it doesn't seem to support the from x import * syntax. Am I wrong?
#
working well on windows.
i'd like to change snippet a bit:
#
oops, guys, you're right. i have to read how to do "from ... import *" with import.
#
Please login first before commenting.