Skip to main content

Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards and optional settings files.

Project description

https://travis-ci.org/sobolevn/django-split-settings.png?branch=master

Organize Django settings into multiple files and directories. Easily override and modify settings. Use wildcards in settings file paths and mark settings files as optional.

Requirements

  • Python 2.7, 3.2, 3.3, 3.4

  • Django >= 1.5, < 1.9

Installation

Install by using pip:

pip install django-split-settings

Usage

Replace your existing settings.py with a list of components that make up your Django settings. Preferably create a settings package that contains all the files.

The GitHub repository contains an example app.

Here’s a minimal example:

from split_settings.tools import optional, include

include(
    'components/base.py',
    'components/database.py',
    optional('local_settings.py'),

    scope=globals()
)

In the example, the files base.py and database.py are included in that order from the subdirectory called components/. local_settings.py in the same directory is included if it exists.

Note: The local context is passed on to each file, so each following file can access and modify the settings declared in the previous files.

Advanced example

Here’s an example of the new settings/__init__.py that also takes into consideration whether the user has supplied another settings module as a command line parameter. It also offers two different ways to override settings in the local installation:

from split_settings.tools import optional, include
import os
import socket

if os.environ['DJANGO_SETTINGS_MODULE'] == 'example.settings':
    # must bypass this block if another settings module was specified
    include(
        'components/base.py',
        'components/locale.py',
        'components/apps_middleware.py',
        'components/static.py',
        'components/templates.py',
        'components/database.py',
        'components/logging.py',

        # OVERRIDE SETTINGS

        # hostname-based override, in settings/env/ directory
        optional('env/%s.py' % socket.gethostname().split('.', 1)[0]),

        # local settings (do not commit to version control)
        optional(os.path.join(os.getcwd(), 'local_settings.py')),

        scope=globals()
    )

The example also tries to include a settings file with the current hostname from the env/ directory for different configurations on each host.

Finally, it tries to locate local_settings.py from the working directory (usually the project root directory, assuming that you called manage.py runserver from there).

Tip: If you’re using Apache and mod_wsgi, you can set the working directory with the home option in the WSGIDaemonProcess directive.

Overriding settings

Files on the inclusion list can override and modify the settings configured in the previous files. For example:

components/base.py:

DEBUG = False
TEMPLATE_DEBUG = DEBUG

MIDDLEWARE_CLASSES = (
    # Your project's default middleware classes
)

INSTALLED_APPS = (
    # Your project's default apps
)

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'example',
        'USER': 'db_user',
        'PASSWORD': 'abc123',
        'HOST': '',
        'PORT': '',
    }
}

local_settings.py:

# Use debug mode locally
DEBUG = True
TEMPLATE_DEBUG = DEBUG

# Add django-debug-toolbar
MIDDLEWARE_CLASSES += ('debug_toolbar.middleware.DebugToolbarMiddleware',)
INSTALLED_APPS += ('debug_toolbar',)

# Use a different database password in development
DATABASES['default']['PASSWORD'] = 'password1'

Tips and tricks

You can use wildcards in file paths:

include(..., 'components/my_app/*.py', ...)

Note that files are included in the order that glob returns them, probably in the same order as what ls -U would list them. The files are NOT in alphabetical order.

Do you want to contribute?

Read the contribute file.

Authors

Changelog

0.1.2

  • Fixed Python 3 compatibility. Fixed issue #7.

0.1.1

  • Fixed issue #1: now works with Gunicorn, too

0.1.0

  • Initial version

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django-split-settings-0.1.2.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

File details

Details for the file django-split-settings-0.1.2.tar.gz.

File metadata

File hashes

Hashes for django-split-settings-0.1.2.tar.gz
Algorithm Hash digest
SHA256 d4b3ef5df90802a68dc79c8fa0dc1e6171d14dd9c6d2f2d853ae5ff8d86ecf46
MD5 28e48fac29878c88f782861a0516eabe
BLAKE2b-256 9d8496ad92b3236b5a12eacd94c3a07a3e3c44caf6c2fcf04d0a7cf0a3e3aea7

See more details on using hashes here.

File details

Details for the file django-split-settings-0.1.2.macosx-10.9-intel.exe.

File metadata

File hashes

Hashes for django-split-settings-0.1.2.macosx-10.9-intel.exe
Algorithm Hash digest
SHA256 8b314f29ce6bbe5d4c5bc2ccaa942504b5a3a6908a8972f454153e6e9dc9c779
MD5 eb8b565bc2b5bba8e77c95234667459d
BLAKE2b-256 caa22584c4254745379a1055203960105522d71d252ce2b603c256add681e30e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page