Hardcoding sensitive credentials—such as database passwords, Stripe API keys, AWS secret tokens, and Django's SECRET_KEY—directly into source code files is one of the most severe security vulnerabilities in software engineering. If source code is pushed to public repositories or shared across teams, exposed credentials allow attackers to compromise production systems. Professional Django architectures use Twelve-Factor App Environment Configuration.
1. The Twelve-Factor Configuration Principle
"Store config in the environment." — The Twelve-Factor App
A codebase should be completely decoupled from environment credentials. The exact same container image or git commit should run in:
Local Development: Reading local SQLite and mock API keys.
Staging: Reading staging PostgreSQL and sandbox payment gateways.
Production: Reading production RDS clusters and live payment credentials.
2. Managing Environments with python-dotenv
In local development, store secrets in a .env file at the root of your project: