Skip to content
Permalink
Browse files

Make env.local to be automatically loaded

Closes #80
  • Loading branch information...
jaywink committed Jan 7, 2017
1 parent a32da6f commit 9c363d2b1a9c576f3f9bf1a6a5ee7695c83d10ac
Showing with 6 additions and 27 deletions.
  1. +1 −7 README.md
  2. +5 −0 config/settings/common.py
  3. +0 −20 manage.py
@@ -56,16 +56,12 @@ The file `requirements.apt` contains other various dependencies. You can use the

#### Configure

Configuration is done via environment variables. For the meaning of them, look them up under files in `config/settings`.
Configuration is done via environment variables. For the meaning of them, look them up under files in `config/settings`. Values in `env_local` will be used automatically.

cp env.example env.local

Edit any values necessary. By default the `SECRET_KEY` is empty. You MUST set something to it. We don't supply a default to force you to make it unique in your production app.

When running `manage.py` commands, these environment variables will be automatically loaded. For other usage (like `py.test`), load the environment in your shell with:

export `cat env.local`

#### Create a database

If you changed the `DATABASE_URL` in the settings file, make sure to change the values in these commands accordingly.
@@ -98,8 +94,6 @@ You should now be able to log in as the user `admin`.

### Running tests

Export your settings to the shell first.

#### Python tests

py.test
@@ -10,12 +10,17 @@
"""
from __future__ import absolute_import, unicode_literals

import os

import environ

ROOT_DIR = environ.Path(__file__) - 3 # (/a/b/myfile.py - 3 = /)
APPS_DIR = ROOT_DIR.path('socialhome')

# Local environment
# -----------------
env = environ.Env()
env.read_env('env.local')

# APP CONFIGURATION
# ------------------------------------------------------------------------------
@@ -1,29 +1,9 @@
#!/usr/bin/env python
import os
import sys
import warnings


if __name__ == "__main__":
# Load environment variables from env.local.
# Good for injecting environment into PyCharm run configurations for example and no need to
# manually load the env values for manage.py commands
try:
with open("env.local") as envfile:
for line in envfile:
if line.strip():
setting = line.strip().split("=", maxsplit=1)
os.environ.setdefault(setting[0], setting[1])
# Backwards compatibility, since we used to require this and Django will freak out if here
# TODO: Remove in some future date
if "--load-env" in sys.argv:
warnings.warn("Argument '--load-env' has been deprecated and will be removed in the future.")
sys.argv.remove("--load-env")
except FileNotFoundError:
warnings.warn("Config file 'env.local' is missing. It should be created from 'env.example' with "
"necessary values replaced.")

# Fail safe settings module
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config.settings.local")

from django.core.management import execute_from_command_line

0 comments on commit 9c363d2

Please sign in to comment.
You can’t perform that action at this time.