How to configure Django settings¶

It is important to understand that in Divio Cloud projects, some settings need to be inspected andmanipulate programatically, to allow the addons system to handle configuration automatically. Seethe Addon-configured settings section for more on this.

This can entail a little extra work when you need to change settings yourself, but the hugeconvenience it offers is more than worth the effort.

The correct way to manage settings such as INSTALLED_APPS is to manipulate the existing value,after having loaded the settings from the addons with aldryn_addons.settings.load(locals()).For example, in the default settings.py you will find:

  1. import aldryn_addons.settings
  2. aldryn_addons.settings.load(locals())
  3.  
  4. INSTALLED_APPS.extend([
  5. # add your project specific apps here
  6. ])

This allows you to add items to INSTALLED_APPS without overwriting existing items, bymanipulating the list.

You will need to do the same for other configured settings, which will include:

  • MIDDLEWARE (or the older MIDDLEWARE_CLASSES)
  • TEMPLATES (or the older TEMPLATE_CONTEXT_PROCESSORS, TEMPLATE_DEBUG and othertemplate settings)
  • application-specific settings, for example that belong to django CMS or Wagtail. See eachapplication’s Application configuration with aldryn_config.py for the settings it will configure.

Inserting an item at a particular position¶

Sometimes it’s not enough just to add an application or class to a list. It may need to beadded before another item. Say you need to add your application security just before cms. In this case you can target cms in the list like this:

  1. INSTALLED_APPS.insert(
  2. INSTALLED_APPS.index("cms") + 0,
  3. "security"
  4. )

(+ 0 will insert the new item "security" immediately before "cms" in the list).

Of course you can use Python to manipulate the collections in any way you require.

Manipulating more complex settings¶

Note that in the case of more complex settings, like MIDDLEWARE or TEMPLATES, which are nolonger simple lists, you can’t just extend them directly with new items, you’ll need to dive intothem to target the right list in the right dictionary, for example:

  1. TEMPLATES[0]["OPTIONS"]["context_processors"].append('my_application.some_context_processor')

原文: http://docs.divio.com/en/latest/how-to/configure-settings.html