Overview
The RestAPI project uses environment variables for configuration management through thedjango-environ package. All sensitive configuration values should be stored in a .env file in the project root directory.
Setup
Create a.env file in your project root based on the .env.example template:
Environment Variables Reference
Application Settings
Controls Django’s debug mode. Set to
True for development and False for production.When DEBUG=True:- CORS allows all origins
- All hosts are allowed
- Detailed error pages are shown
DEBUG=False:- CORS is restricted to specific origins
- Only whitelisted hosts are allowed
- Generic error pages are shown
Django’s secret key used for cryptographic signing. This must be kept secret in production.Example:
django-5a5s4d4as44vcu8cnasuau8sg!Generate a secure secret key using Django’s built-in tool:
Database Configuration
The name of your PostgreSQL database.Example:
migoPostgreSQL database username.Example:
postgresPostgreSQL database password for the specified user.Example:
adminPostgreSQL server host address.Example:
localhost or 127.0.0.1PostgreSQL server port number.Example:
5432 (default PostgreSQL port)Example Configuration
Here’s a complete example.env file:
Loading Environment Variables
The application loads environment variables inRestAPI/settings.py:
RestAPI/settings.py
Best Practices
- Development: Use
DEBUG=Trueand local database credentials - Production: Always use
DEBUG=Falseand secure credentials - Secret Key: Generate unique keys for each environment
- Passwords: Use strong, random passwords for database users
- Version Control: Never commit
.envfiles to Git
Troubleshooting
Missing Environment Variables
If you see errors about missing environment variables:- Verify
.envfile exists in the project root - Check all required variables are defined
- Ensure no extra spaces around
=signs - Restart the Django development server after changes
Boolean Values
For theDEBUG variable, use:
DEBUG=TrueorDEBUG=1for developmentDEBUG=FalseorDEBUG=0for production