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'
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.
- [Django]-Fastest way to get the first object from a queryset in django?
- [Django]-How to use Django to get the name for the host server?
- [Django]-How do you catch this exception?
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'
- [Django]-Cannot import models from another app in Django
- [Django]-How to add new languages into Django? My language "Uyghur" or "Uighur" is not supported in Django
- [Django]-Django query where in
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))
- [Django]-Disconnect signals for models and reconnect in django
- [Django]-Dynamically adding a form to a Django formset
- [Django]-Celery task schedule (Ensuring a task is only executed one at a time)
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']))
- [Django]-UnicodeEncodeError: 'ascii' codec can't encode character
- [Django]-Django model fields validation
- [Django]-Django: check whether an object already exists before adding
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))))
- [Django]-Django-Rest-Framework. Updating nested object
- [Django]-Django self-referential foreign key
- [Django]-Why middleware mixin declared in django.utils.deprecation.py
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'
- [Django]-How do I extend the Django Group model?
- [Django]-Django Tastypie Advanced Filtering: How to do complex lookups with Q objects
- [Django]-Is BigTable slow or am I dumb?
- [Django]-Django models avoid duplicates
- [Django]-Can Django automatically create a related one-to-one model?
- [Django]-Django form โ set label