django-environ Configuration

0.13.0 · active · verified Thu Apr 09

django-environ is a Python library that allows Django applications to be configured using 12-factor inspired environment variables. It simplifies parsing various types of settings (e.g., databases, caches, emails, booleans, integers) from `os.environ` or `.env` files into Django-compatible formats. The current version is 0.13.0, with a release cadence that generally follows Django versions and addresses bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize `django-environ`, set up default type casting, and access various types of environment variables including complex ones like database and cache URLs. It simulates environment variables for a runnable example without requiring a physical `.env` file to be present.

import environ
import os

# --- Simulate environment variables for a runnable example ---
# In a real application, these would come from your actual .env file or OS environment.
# For local testing, you might create a .env file like:
# SECRET_KEY=your-super-secret-key-from-env
# DEBUG=True
# DATABASE_URL=sqlite:///myproject.sqlite3
# EMAIL_URL=smtp://user:password@smtp.example.com:587
# CACHE_URL=redis://localhost:6379/1
# ------------------------------------------------------------

os.environ.setdefault('SECRET_KEY', 'your-super-secret-key-for-dev-fallback')
os.environ.setdefault('DEBUG', 'True')
os.environ.setdefault('DATABASE_URL', 'sqlite:///myproject.sqlite3')
os.environ.setdefault('EMAIL_URL', 'smtp://user:password@smtp.example.com:587')
os.environ.setdefault('CACHE_URL', 'redis://localhost:6379/1')

# Initialize the Env object.
# You can set default types and values here if not found in .env or os.environ.
env = environ.Env(
    # default type for DEBUG is bool, default value is False if not set
    DEBUG=(bool, False)
)

# Optional: Explicitly read .env file. By default, Env.read_env() looks for .env
# in the current directory and its parents. If you don't call this, it implicitly
# reads it on the first call to env() or similar, but explicit is better for control.
# Note: This line assumes a .env file exists. For this example, we're relying on os.environ.setdefault.
# environ.Env.read_env()

# Accessing environment variables with type casting
SECRET_KEY = env('SECRET_KEY')
DEBUG = env('DEBUG') # Uses the (bool, False) casting defined above

# Complex settings like database or cache URLs are parsed into Django-compatible dictionaries
DATABASES = {
    'default': env.db() # uses DATABASE_URL from environment
}
CACHES = {
    'default': env.cache() # uses CACHE_URL from environment
}
EMAIL = env.email() # uses EMAIL_URL from environment

print(f"SECRET_KEY: {SECRET_KEY}")
print(f"DEBUG: {DEBUG} (type: {type(DEBUG)})")
print(f"DATABASES (default): {DATABASES['default']}")
print(f"CACHES (default): {CACHES['default']}")
print(f"EMAIL (default): {EMAIL}")

# Example of a missing variable with a default
APP_VERSION = env('APP_VERSION', default='1.0.0')
print(f"APP_VERSION: {APP_VERSION}")

view raw JSON →