该DJANGO_CONFIGURATION
变量引用一个configurations.Configuration
子类。如有关使用模式的文档中所述,其想法是用维护多个设置文件的繁琐开销来换取更多 DRYer Mixin/Class 工作流程方案。我知道不得不去改变很烦人manage.py
,wsgi.py
但你会得到很多。
# example settings.py
from configurations import Configuration, values
# Build up some mixin classes for related or app-specific settings:
class AppsMixin(object):
DJANGO_APPS = (
'django.contrib.auth', 'django.contrib.contenttypes',
'django.contrib.sessions', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.sites',
'django.contrib.flatpages', 'django.contrib.sitemaps',
'django_extensions'
)
ADMIN_APPS = ('filebrowser', 'grappelli', 'django.contrib.admin',)
DEV_APPS = ('django.contrib.admindocs', 'debug_toolbar',)
DEFAULT_APPS = (
'tagging', 'imagekit',
'tinymce', 'ajax_select',
'crispy_forms', #...
)
@property
def INSTALLED_APPS(self):
""" Control application ordering dynamically """
OUT_APPS = self.DJANGO_APPS + self.ADMIN_APPS
if self.DEBUG:
OUT_APPS += self.DEV_APPS
return OUT_APPS + self.DEFAULT_APPS
class AuthURLMixin(object):
LOGIN_REDIRECT_URL = 'site-login-success'
LOGIN_URL = '/auth/login/'
LOGOUT_URL = '/auth/logout/'
class CrispyFormsMixin(object):
""" django-crispy-forms specific settings """
CRISPY_TEMPLATE_PACK = 'bootstrap3'
@property
def CRISPY_FAIL_SILENTLY(self):
return not self.DEBUG
class Base(AppsMixin, AuthURLMixin, CrispyFormsMixin, Configuration):
""" Your equivalent for settings/base.py """
pass
class Local(Base):
""" ~ settings/local.py """
DEBUG = True
TEMPLATE_DEBUG = DEBUG
# Custom setting that lets subclasses or your apps
# check which subclass is active.
STAGING = False
# Enable a setting to be defined in os.environ, with a sensible default
# Don't forget the 'DJANGO_' prefix (e.g. DJANGO_TIME_ZONE)
TIME_ZONE = values.Value('America/New_York')
HTTPS_ONLY = False
# Stash the secret key in os.environ as DJANGO_SECRET_KEY
SECRET_KEY = values.SecretValue()
@property
def CSRF_COOKIE_SECURE(self):
""" chained dynamic setting """
return self.HTTPS_ONLY
@property
def SESSION_COOKIE_SECURE(self):
""" chained dynamic setting """
return self.HTTPS_ONLY
class Staging(Local):
""" This class is used for testing before going production """
STAGING = True
TIME_ZONE = values.Value('America/Phoenix')
#...
class Prod(Staging):
""" ~ settings/production.py """
DEBUG = False
STAGING = False
HTTPS_ONLY = True
然后,从您的笔记本电脑:
/path/to/project/$ python manage.py shell --configuration=Local
>>>from django.conf import settings
>>>settings.DEBUG, settings.STAGING, settings.TIME_ZONE
(True, False, 'America/New_York')
从您的远程服务器:
/path/to/remote/project/$ python manage.py shell --configuration=Staging
>>>from django.conf import settings
>>>settings.DEBUG, settings.STAGING, settings.TIME_ZONE
(True, True, 'America/Phoenix')
一旦一切都完美,进入系统范围:
# /etc/environment
DJANGO_SETTINGS_MODULE=thisproject.settings
DJANGO_CONFIGURATION=Prod
现在,三文件问题已经通过一些附加功能得到解决:
- 动态属性允许您通过实例方法一次切换或插入任意数量的相关设置。
- 可以通过 mixins 引入和删除设置块。
- 敏感键设置不受源代码控制。
因此,您可以想象制作其中三个文件,每个文件包含三个类,但为什么不只制作一个包含九个类的文件呢?