Environment Variables and Settings Split
Separate dev and prod configuration safely.
Environment Variables and Settings Split is a free Django Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
One Settings File Is Risky
A single settings.py with DEBUG on and secrets hardcoded is fine locally but dangerous live. Production needs its own safe configuration. 🔒
Dev and Prod Differ
Local and live environments need different settings: debug, allowed hosts, databases, and email all change between the two.
Why Environment Variables
Secrets like passwords must never sit in code or git. Environment variables inject them at runtime, keeping them out of your repository.
Read with os.environ
Pull a value from the environment with os.environ. Use a fallback so missing local variables do not crash development.
import os
SECRET_KEY = os.environ.get('SECRET_KEY', 'dev-only')Load a .env File
Tools like python-dotenv load variables from a local .env file, so you do not export them by hand every session.
from dotenv import load_dotenv
load_dotenv()Never Commit .env
Add .env to your .gitignore. Committing it would leak every secret, defeating the entire reason for using environment variables.
.env
*.pycThe Settings Package
Split settings into a package: turn settings.py into a folder holding base, dev, and prod modules that share common config.
settings/
__init__.py
base.py
dev.py
prod.pyShare with base.py
Put everything common in base.py. The dev and prod files import from it, then override only what truly differs.
from .base import *
DEBUG = TrueLock Down prod.py
In prod.py keep DEBUG off and read SECRET_KEY plus ALLOWED_HOSTS from the environment, never from a literal in the file.
from .base import *
DEBUG = False
ALLOWED_HOSTS = os.environ['ALLOWED_HOSTS'].split(',')Pick the Module
Choose which settings load with the DJANGO_SETTINGS_MODULE variable. Dev points to settings.dev while the server points to settings.prod.
export DJANGO_SETTINGS_MODULE=myproject.settings.prodTyped Helpers Help
Libraries like django-environ parse env values into booleans, lists, and database URLs, so you avoid messy manual string conversions.
Quick Check
Where should your production SECRET_KEY come from?
Recap
You learned to split settings into base, dev, and prod, keep secrets in environment variables and .env, and select the module per environment. 🎯
Frequently asked questions
Is the “Environment Variables and Settings Split” lesson free?
Yes — the full text of “Environment Variables and Settings Split” is free to read here on the web, and the Django Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Django Academy course, upgrade to CoddyKit PRO.
What will I learn in “Environment Variables and Settings Split”?
Separate dev and prod configuration safely. You practise Django Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Django Academy?
No prior experience is required. Django Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Environment Variables and Settings Split” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Django Academy lesson?
Yes. Every Django Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Gunicorn as the WSGI Server
- Nginx as a Reverse Proxy
- WhiteNoise for Static Files
- Environment Variables and Settings Split