您应该创建一个“clients”文件夹,并为每个客户端创建一个子目录。在每个子目录中,创建一个 site_settings.py 文件,如下所示:
import os.path
# import global settings
from settings import *
# this is barely just the name of the client dir, you might want to use that
SITE_NAME = __file__.split('/')[-2]
# this is the directory of the client website
CLIENT_ROOT = os.path.abspath(os.path.dirname(__file__))
DATABASES = {
'default': {
'ENGINE': 'django.contrib.gis.db.backends.postgis',
'NAME': SITE_NAME,
'USER': SITE_NAME,
'PASSWORD': 'some random password',
}
}
# you might want this too so that each client have his own MEDIA_ROOT
MEDIA_ROOT = os.path.join(CLIENT_ROOT, 'upload')
然后不要忘记使用 --settings 开关来执行管理命令。例如:
./manage.py syncdb --settings=clients.demo.site_settings
不要忘记每个客户都需要有自己的额外东西。例如,如果您将 haystack 与 whoosh 结合使用,则需要添加以下内容,以免在客户端之间混淆:
HAYSTACK_WHOOSH_PATH = os.path.join(CLIENT_ROOT, 'whoosh')
或者使用 django-zstask:
ZTASKD_URL = 'ipc:///tmp/%s_ztask.sock' % SITE_NAME
或者使用 JohnnyCache:
JOHNNY_MIDDLEWARE_KEY_PREFIX=SITE_NAME