Automate raising ImproperlyConfigure if environment variable is missing

Viewed 18

I have the following code in one of my views.py:

tl_key = os.getenv("TRANSLOADIT_KEY")
tl_secret = os.getenv("TRANSLOADIT_SECRET")

if not tl_key:
    logger.critical("TRANSLOADIT_KEY not set")
    raise ImproperlyConfigured

if not tl_secret:
    logger.critical("TRANSLOADIT_SECRET not set")
    raise ImproperlyConfigured

I know that if Django doesn't find SECRET_KEY or DEBUG environment variable, it will raise ImproperlyConfigured exception. Is there a way I can specify which env variables are required so that the said exception is raised automatically?

1 Answers

You can add another environment variable defining your need, something like APP_ENVIRONMENT=dev|prod, that way you can check that var and raise ImproperlyConfigured or assign a default value in your code

Related