[Django]-Django ignoring DEBUG value when I use os.environ, why?

50๐Ÿ‘

โœ…

The value of os.environ['DEBUG_VALUE'] is a string and bool('non empty string') == True.

You should do something similar to:

DEBUG = os.environ['DEBUG_VALUE'] == 'TRUE'
๐Ÿ‘คaumo

17๐Ÿ‘

The django-environ package has a simple way of managing this that is more robust and elegant, I think, than having to manually parse the string value (which will always evaluate to true) โ€“ you can import your environment as an object.

Export the environment variable and install the package:

export MY_DEBUG_ENV_VAR=False
pip install django-environ

Then in django, import the environment as an Env() object and use the bool() method to parse a boolean and provide an optional default value:

import environ
env = environ.Env()
MY_DEBUG_ENV_VAR = env.bool('MY_DEBUG_ENV_VAR', default=False)

Tada! The Env() object also has a bunch of other methods (e.g. for parsing integers, floats, strings etc etc).

NB I found this though the django-cookiecutter application, which has a bunch of equally useful things preinstalled and is a great starting point for projects whether youโ€™re new or experienced with django.

๐Ÿ‘คthclark

5๐Ÿ‘

Maybe you want something more forgiving. First allow the local definition for development purposes. And only if not defined, get it from environment variable, but use the case insensitive comparison (since the person doing the deployment might not be the developer writing this line of code).

try:
    DEBUG = DEBUG_VALUE_LOCAL
except NameError:
    DEBUG = os.environ.get('DEBUG_VALUE').lower() == 'true'
๐Ÿ‘คdsalaj

2๐Ÿ‘

Alternatively, you can evaluate with int(String), if you use 1/0 values, instead of True/False for the environment variable:

# Set DEBUG flag. Default is 0 (considered as False in conditions)
DEBUG = int(os.environ.get('DEBUG_VALUE', 0)) 
๐Ÿ‘คNoam Manos

2๐Ÿ‘

Another solution, in my opinion best would be use strtobool from distutils.util

In python 3 and above

#import distutls 
# if you using python version > 3 use from distutils import strtobool
from distutils.util import strtobool 

_DEBUG = bool(strtobool(os.environ['DEBUG_MODE']))
๐Ÿ‘คShajibur Rahman

1๐Ÿ‘

I know it is an old post but this has worked for me ALWAYS when I use a .env with a DEBUG variable set to either 0 or 1.

DEBUG = (bool(int(os.environ.get('DEBUG',1))))
๐Ÿ‘คLuis Valverde

0๐Ÿ‘

I confirm that the method provided by @aumo (DEBUG = os.environ[โ€˜DEBUG_VALUEโ€™] == โ€˜TRUEโ€™) works fine even if you need to pass the parameters on systemd init file
eg:

[Unit]
Description=Sample Application using Daphne
After=network.target

[Service]
Type=simple
Environment=DEBUG=False
Environment=LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/lib/

User=root
WorkingDirectory=/var/projects/sampleapp/app
ExecStart=/var/projects/sampleapp/env/bin/daphne -b 0.0.0.0 -p 8000 app.asgi:application
Restart=always

[Install]
WantedBy=multi-user.target

and in settings file something like this:

DEBUG = os.getenv('DEBUG') == 'True'
๐Ÿ‘คJohn Anderton

0๐Ÿ‘

Robust logic to this would be:

DEBUG = os.environ.get('DEBUG', False) == 'True'
๐Ÿ‘คAnonimy

Leave a comment