Code Monkey home page Code Monkey logo

django-env-overrides's Introduction

Overview

Override arbitrary Django settings via environment variables.

  • Free software: BSD license
tests
Travis-CI Build Status
Coverage Status
package PyPI Package latest release PyPI Package monthly downloads PyPI Wheel Supported versions Supported implementations

When to use this package

This package lets you override any Django setting using environment variables, just by adding a couple of lines to the bottom of settings.py.

This is handy if:

  • You have a project that is not currently configurable via environment variables, and you want to quickly adapt it to run in an environment like Heroku.
  • You want to be able to quickly override settings on deployed code for debugging.

This is not a good idea as the primary way of configuring a complex project in production. In general you'll be happier keeping all settings in source control, and explicitly recording which settings need to be provided by environment variables using django-environ.

Installation

pip install django-env-overrides

Documentation

django-env-overrides lets you quickly adjust an existing Django app to load arbitrary settings from environment variables. It uses django-environ to parse settings from the environment, but allows override of arbitrary settings without specific changes to settings.py.

Setup

Add these lines to the end of your settings.py file:

import django_env_overrides
django_env_overrides.apply_to(globals())

Any environment variable prefixed with DJANGO__ will now be imported to your settings.

Example

settings.py:

DEBUG = True
MEDIA_URL = '/media/'
DATABASES = {
    'default': {
        'ENGINE': 'sqlite3',
    }
}
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
            ]
        }
    }
]

import django_env_overrides
django_env_overrides.apply_to(globals())

Environment:

DJANGO__SECRET_KEY=secret
DJANGO__MEDIA_URL=/new_url/
DJANGO__bool__DEBUG=False
POSTGRES=postgres://uf07k1:[email protected]:5431/d8r82722
DJANGO__db__DATABASES__default=$POSTGRES
DJANGO__TEMPLATES__0__OPTIONS__context_processors__1='my.context.processor'

Result:

DEBUG = False
MEDIA_URL = '/new_url/'
SECRET_KEY = 'secret'
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'd8r82722',
        'HOST': 'ec2-107-21-253-135.compute-1.amazonaws.com',
        'USER': 'uf07k1',
        'PASSWORD': 'wegauwhg',
        'PORT': 5431,
    }
}
TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'OPTIONS': {
            'context_processors': [
                'django.contrib.auth.context_processors.auth',
                'my.context.processor',
            ]
        }
    }
]

Format for environment variables

The format for environment variable names is:

<prefix>__<typecast>__<path>__<to>__<target>__<setting>

<prefix> defaults to DJANGO. If you want to use another prefix, use django_env_overrides.apply_to(globals(), prefix="MYPREFIX").

<typecast> (optional) is any type known to the django-environ package. Currently the supported types are str, bool, int, float, json, list, tuple, dict, url, path, db_url, cache_url, search_url, and email_url. See the django-environ package for usage. If <typecast> is omitted, values are set as str.

<path>__<to>__<target>__<setting> specifies the setting or subsetting the value should be assigned to. Path elements are treated as array indexes if they are integers, and otherwise as dictionary keys.

Development

See CONTRIBUTING.rst

django-env-overrides's People

Contributors

jcushman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

django-env-overrides's Issues

Concatenation with env substitution

Hi !
Actually when I do (in env):

VAR=test
VAR2=$VAR

Working well

BUT

VAR=test
VAR2=addSomething$VAR

Doesn't work

Is it possible to have concatenation with variable substitution to work ?

Best regards,
Lucas

Env variables subtitution

Hello,

Thx for this project it is very useful. I have a question: in your example you have
POSTGRES=postgres://uf07k1:[email protected]:5431/d8r82722 DJANGO__db__DATABASES__default=$POSTGRES
But when I try to do the same the variable $POSTGRES is not replace. How it's work ?

Best regards,

Lucas

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.