Configure Django using environment variables.
Project description
django-envconfig
Configure Django using environment variables. Having a settings.py
file is
optional.
Getting started
Installation
python -m pip install django-envconfig
Usage
Edit the manage.py
, asgi.py
and wsgi.py
files generated by Django's
startproject
command and change the following line:
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'envconfig.settings')
Minimum configuration
The following environment variable is required:
export DJANGO_PROJECT=my_project_name
In the absence of a settings.py
file the following environment variables are
required: ALLOWED_HOSTS
and either DATABASES
or PGDATABASE
.
Environments may be stored in an .env
file. This file can be stored in your
root directory (same as manage.py
) or anywhere on the path (e.g. virtualenv
directory).
How it works
Any Django setting can be configured as an environment variable.
- To set a boolean:
true|yes|on|1
andfalse|no|off|0
(case-insensitive) - To set
None
:none|null
(case-insensitive) - Simple lists of strings can be stored comma-separated e.g.
export ALLOWED_HOSTS=127.0.0.1,localhost
- Dicts and complex lists should be stored as JSON
Settings are loaded with the following priority (highest first):
- Environment variables.
- Settings defined in your projects
settings.py
, if it exists. - Settings that would be defined by a
settings.py
file generated bystartproject
. This should eliminate the need for the file in most projects. Two main caveats:- The default value for
DEBUG
has been changed toFalse
. - A
SECRET_KEY
is generated but will not persist between sessions (e.g. if you restart your server/process manager). This may or may not be an issue depending on your use case - see the Django documentation for more details.
- The default value for
If you are using a PostgreSQL backend you do not need to specify a DATABASES
setting if you have set PostgreSQL environment variables - the minimum is
PGDATABASE
. See the PostgreSQL docs
for more details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django_envconfig-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 68a3962fe11e27d401fae3acb4e41679c6defd6fbd858eb3273f5e13152f2557 |
|
MD5 | 7ac62c7c40b437fc8d4cf36cbc6abd64 |
|
BLAKE2b-256 | da570c82111b497a816cf73e25c8e10d72bf03d4c258368931250aeeb82accd3 |