Django ignoring DEBUG value when I use os.environ, why?
Question:
In my Django settings I have the following:
DEBUG = os.environ[‘DEBUG_VALUE’]
Where DEBUG_VALUE = False
However, Django continues to show full error messages when I do this. If I manual add DEBUG = False
it works and shows 500 error.
For some reason Django is ignoring it when I use the os.environ
value.
I have confirmed DEBUG_VALUE
is False but outputting to a file.
I have even tried:
DEBUG = bool(os.environ[‘DEBUG_VALUE’])
and still shows full errors.
Answers:
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'
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'
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.
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))
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']))
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))))
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'
In my Django settings I have the following:
DEBUG = os.environ[‘DEBUG_VALUE’]
Where DEBUG_VALUE = False
However, Django continues to show full error messages when I do this. If I manual add DEBUG = False
it works and shows 500 error.
For some reason Django is ignoring it when I use the os.environ
value.
I have confirmed DEBUG_VALUE
is False but outputting to a file.
I have even tried:
DEBUG = bool(os.environ[‘DEBUG_VALUE’])
and still shows full errors.
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'
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'
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.
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))
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']))
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))))
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'