I have a Django application where I'm handling environment variables using python-decouple and separate .env
files. This works fine for variables that exist in both development and production environments, such as DEBUG
.
SECRET_KEY = config('SECRET_KEY')
DEBUG = config('DEBUG', cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=Csv())
While DEBUG
has distinct values in each environment, other variables like SECURE_HSTS_SECONDS
only need to be set in production and do not need to be set at all in development. I'm currently just hard-coding these values in my settings.py file:
if not DEBUG:
SECURE_HSTS_SECONDS = 60
SECURE_HSTS_INCLUDE_SUBDOMAINS = True
SECURE_REFERRER_POLICY = 'same-origin'
SECURE_HSTS_PRELOAD = True
SECURE_SSL_REDIRECT = True
SESSION_COOKIE_SECURE = True
CSRF_COOKIE_SECURE = True
I suppose I could include these values in my dev .env
file and just set them to their default values, but that seems unnecessary. Is there a cleaner way to implement this or a best practice? I'd prefer solutions that work with python-decouple