Django deployment

12 views
Skip to first unread message

vishy

unread,
Jun 3, 2009, 4:16:01 AM6/3/09
to Django users
Hi,

I am developing an application on windows. I decided to upload it on
webfaction n see how deployment goes. The issues I faced was with
paths given -
for templates - I had given absolute path of directory on windows,
for database(using sqlite) - just the name.But, for deployment I had
to change both the paths. Is there any way which I can avoid this?

thanks

Jarek Zgoda

unread,
Jun 3, 2009, 4:54:21 AM6/3/09
to django...@googlegroups.com
Wiadomość napisana w dniu 2009-06-03, o godz. 10:16, przez vishy:


You can use separate settings files for development and deployment,
this is the way most people do deployment-specific configurations.

For example, you can have main settings module that imports all names
from settings_deployment and settings_local, witch catched ImportError
exceptions, like:

<settings.py>

try:
from settings_deployment import *
except ImportError:
# not a prod environment
pass
try:
from settings_local import *
except ImportError:
# no local specific modifications
pass

Putting these imports at the bottom of settings.py will cause the
values will be overwritten with imported ones.

--
Artificial intelligence stands no chance against natural stupidity

Jarek Zgoda, R&D, Redefine
jarek...@redefine.pl

Daniel Roseman

unread,
Jun 3, 2009, 5:26:11 AM6/3/09
to Django users
Jarek has shown you one way to do it, by having separate local
settings files.

Another way is to set the template path dynamically in settings.py:

import os.path
OUR_ROOT = os.path.dirname(os.path.realpath(__file__))
TEMPLATE_DIRS = (
os.path.realpath(os.path.join(OUR_ROOT, 'templates')),
)

I prefer this way as it means you don't depend on files that aren't in
version control - although you might still use the local settings
trick to override other settings, like database name/path.
--
DR.

Jashugan

unread,
Jun 3, 2009, 10:59:35 AM6/3/09
to Django users
On Jun 3, 1:16 am, vishy <vishalsod...@gmail.com> wrote:
> for templates - I had given absolute path of directory on windows,
> for database(using sqlite) - just the name.But, for deployment I had
> to change both the paths. Is there any way which I can avoid this?
>
> thanks

Another method is to sniff the hostname:

from socket import gethostname

PRODUCTION = gethostname() in ('server_name',)

if PRODUCTION:
# set production paths here
else:
# set development paths here

Kegan Gan

unread,
Jun 3, 2009, 11:57:22 AM6/3/09
to Django users
This is how I did it. You have have a settings_default.py, which
contains all the commons settings.

In your development environment, you use settings.py that has this on
the top ...

from settings_defaults import *

... then you overwrite whatever setting variables that are needed for
your development environment.

For you production (server) environment, you use another settings.py
again import from settings_default, and overwrite whatever setting
variables that are needed for the server.

Also, you can use the following to get the path on where your django
project is:

import os.path
PROJECT_DIR = os.path.dirname(__file__)

Hope this helps.

AmanKow

unread,
Jun 4, 2009, 10:53:15 AM6/4/09
to Django users
One more way?

# Get any additional settings modules from the
ADDITIONAL_SETTINGS_MODULES
# environment variable, if set. These files should exist in the same
# directory as the main settings module. Use standard module notation
# (eg. 'prod_settings' without adding a .py to the end).
#
# Multiple additional modules can be listed, separated by
# a comma ',' and will be imported in order. Any settings defined
# by these files will clobber existing settings, including those in
the main
# (this) settings file.

settings_modules = os.environ.get('ADDITIONAL_SETTINGS_MODULES', '')

for settings_module in settings_modules.split(','):
settings_module = settings_module.strip()
if settings_module:
exec 'from %s import *' % (settings_module,)

I have the above at the bottom of my settings module.

This allows me to keep all of my settings file in source control, and
to select the appropriate settings by via the environment. I find
this fairly flexible.

Wayne
Reply all
Reply to author
Forward
0 new messages