Home:ALL Converter>Split configuration files using django-configurations

Split configuration files using django-configurations

Ask Time:2013-11-13T03:29:41         Author:Richard

Json Formatter

A package built for Django, django-configurations extends module based setting loading with object oriented patterns.

The package uses DJANGO_SETTINGS_MODULE and DJANGO_CONFIGURATION environment variables to identify a settings file and load the appropriate configuration respectively.

I would like to split the configuration (each object) into separate files but experiencing difficulty.

Currently;

    settings/settings.py

Would like to change to;

    settings/base.py  
    settings/local.py  
    settings.production.py

Has anyone managed to achieve this, or perhaps know a working solution to achieve this?

Author:Richard,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/19938192/split-configuration-files-using-django-configurations
Jake Gwyn :

The DJANGO_CONFIGURATION variable refers to a configurations.Configuration subclass. As described in the docs on usage patterns, the idea is to trade in the tedious overhead of maintaining multiple settings files for a much DRYer Mixin/Class workflow scheme. I know it's annoying having to go change manage.py and wsgi.py, but you get a lot for it.\n\n# example settings.py\nfrom configurations import Configuration, values\n\n# Build up some mixin classes for related or app-specific settings:\nclass AppsMixin(object):\n DJANGO_APPS = (\n 'django.contrib.auth', 'django.contrib.contenttypes',\n 'django.contrib.sessions', 'django.contrib.messages', \n 'django.contrib.staticfiles', 'django.contrib.sites',\n 'django.contrib.flatpages', 'django.contrib.sitemaps',\n 'django_extensions'\n )\n ADMIN_APPS = ('filebrowser', 'grappelli', 'django.contrib.admin',)\n DEV_APPS = ('django.contrib.admindocs', 'debug_toolbar',)\n DEFAULT_APPS = (\n 'tagging', 'imagekit',\n 'tinymce', 'ajax_select',\n 'crispy_forms', #...\n )\n\n @property\n def INSTALLED_APPS(self):\n \"\"\" Control application ordering dynamically \"\"\"\n OUT_APPS = self.DJANGO_APPS + self.ADMIN_APPS\n if self.DEBUG:\n OUT_APPS += self.DEV_APPS\n return OUT_APPS + self.DEFAULT_APPS\n\nclass AuthURLMixin(object):\n LOGIN_REDIRECT_URL = 'site-login-success'\n LOGIN_URL = '/auth/login/'\n LOGOUT_URL = '/auth/logout/'\n\nclass CrispyFormsMixin(object):\n \"\"\" django-crispy-forms specific settings \"\"\"\n CRISPY_TEMPLATE_PACK = 'bootstrap3'\n\n @property\n def CRISPY_FAIL_SILENTLY(self):\n return not self.DEBUG\n\nclass Base(AppsMixin, AuthURLMixin, CrispyFormsMixin, Configuration):\n \"\"\" Your equivalent for settings/base.py \"\"\"\n pass\n\nclass Local(Base):\n \"\"\" ~ settings/local.py \"\"\"\n DEBUG = True\n TEMPLATE_DEBUG = DEBUG\n # Custom setting that lets subclasses or your apps\n # check which subclass is active.\n STAGING = False\n # Enable a setting to be defined in os.environ, with a sensible default\n # Don't forget the 'DJANGO_' prefix (e.g. DJANGO_TIME_ZONE) \n TIME_ZONE = values.Value('America/New_York')\n HTTPS_ONLY = False\n # Stash the secret key in os.environ as DJANGO_SECRET_KEY\n SECRET_KEY = values.SecretValue()\n\n @property\n def CSRF_COOKIE_SECURE(self):\n \"\"\" chained dynamic setting \"\"\"\n return self.HTTPS_ONLY\n\n @property\n def SESSION_COOKIE_SECURE(self):\n \"\"\" chained dynamic setting \"\"\"\n return self.HTTPS_ONLY\n\nclass Staging(Local):\n \"\"\" This class is used for testing before going production \"\"\"\n STAGING = True \n TIME_ZONE = values.Value('America/Phoenix')\n #...\n\nclass Prod(Staging):\n \"\"\" ~ settings/production.py \"\"\"\n DEBUG = False\n STAGING = False\n HTTPS_ONLY = True\n\n\nThen, from your laptop:\n\n/path/to/project/$ python manage.py shell --configuration=Local\n>>>from django.conf import settings\n>>>settings.DEBUG, settings.STAGING, settings.TIME_ZONE\n(True, False, 'America/New_York') \n\n\nFrom your remote server:\n\n/path/to/remote/project/$ python manage.py shell --configuration=Staging\n>>>from django.conf import settings\n>>>settings.DEBUG, settings.STAGING, settings.TIME_ZONE\n(True, True, 'America/Phoenix')\n\n\nOnce everything is perfect, go system-wide:\n\n# /etc/environment\nDJANGO_SETTINGS_MODULE=thisproject.settings\nDJANGO_CONFIGURATION=Prod\n\n\nNow the three-file problem has been solved with a few extras:\n\n\nThe dynamic properties allow you to toggle or interpolate any number\nof related settings at once through instance methods.\nChunks of settings can be introduced and removed through the mixins.\nSensitive key settings are kept out of source control.\n\n\nSo, you could conceivably make three of these files with three classes each but why not just make one with nine?",
2015-01-31T22:46:01
elssar :

You could set a environment variable to specify which settings file to use, and then in yourmanage.py and wsgi.py, do something like this\n\nconfig_mode = os.getenv('DJANGO_CONFIG_MODE', 'base')\n\nconfig_dict = {\n 'base': 'settings.base'\n 'local': 'settings.local'\n 'production': 'settings.production'\n}\n\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', config_dict[config_mode])\n\n# or if you prefer not using a dictionary or if-else blocks, you could\n# set the settings file name you wish to use as the DJANGO_CONFIG_MODE environment\n# variable and use that directly\n\nos.environ.setdefault('DJANGO_SETTINGS_MODULE', 'settings.{0}'.format(config_mode))\n\n\nYou might also want to specify the mode in your different settings files, so that your code can behave differently in different modes.\n\nCONFIG_MODE = 'base' # or 'local' or 'production'\n\n\nin your various settings files, and use it in your code base as required.",
2013-11-12T19:53:29
yy