开发者

Build Automation for Django

I'm pretty new to doing sysadmin stuff for my development and to the django framework. I want to have a different username/pas开发者_如何学Gosword for my local dev station and to my production environment.

I'm using dotcloud as the server. I can write a post install script (in python, bash, whatever) and it will execute it on every new push.

However I don't know how to go about this. Do I need to write this myself? is there a python/django build automation tools that will help me with that?

Clarification: how can I change debug=false in settings.py to true just on the server?


The django standard way is to use an environmanet variable DJANGO_SETTINGS_MODULE. Point it to different settings and let both import a common settings module for common things:

# settings_production.py

from settings_common import *
DEBUG = False
DATABASES = {...}


# settings_development.py

from settings_common import *
DEBUG = True
DATABASES = {...}


# settings_common.py

INSTALLED_APPS = (...) # etc

You can also use an alternative strategy of using one main settings and import names from another one depending on some system condition, like getting os.platform.node() or socket.gethostname() and switch over that value (or part of it).

reversed_hostname_parts = socket.gethostname().split('.').reverse()
host_specific = {
    ('com', 'dotcloud'): 'production',
    ('local'): 'dev',
}

for index in range(len(reversed_hostname_parts)):
    identifier = tuple(reversed_hostname_parts[:index+1])
    if identifier in host_specific:
        extra_settings = host_specific[identifier]
        break
else: # executed when the loop has not been `break`ed
    extra_settings = 'dev'  # any default value


if extra_settings == 'dev':
    from development_settings import *
elif extra_settings == 'production':
    from production_settings import *

EDIT: added link

See https://code.djangoproject.com/wiki/SplitSettings for other strategies.


I usually import my development settings at the end of production settings.py, if my project is residing on a local directory structure.

You can also store your DB settings and other settings that are different in production and development in a separate file and remove them from your SVN, Git of whatever you use.

Just add this at the end of your settings.py:

try:
  from myapp.specific_settings import *
except ImportError:
  pass

In this case, specific_settings will be different in production and development environment.

If you want to dynamically choose between development and production servers use this at the end of settings:

import os
directory = os.path.dirname(__file__)
if directory == '/home/yourname/development/':
    from myapp.development_settings import *
else:
    from myapp.production_settings import * 

Note that I wrote this on top of my head and there might be some bugs in it. I will verify that when I go home.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜