Django ignoring DEBUG value when I use os.environ?
In my Django settings I have the following:
on env.py I have the below code:
os.environ.setdefault( "DEBUG", "True" )
On settings.py I have the below code: DEBUG = os.environ.get('DEBUG') == 'True'
However some of the systes are not visbile after deploying but they are perfect on my local server.
Please assist thank you.
I tried changing the DEBUG value to false but nothing changes.
As per django-environ docs
try something like this
.env
DEBUG=on
DB_NAME=LOCAL_DB_NAME
settings.py
import environ
# cast default value
env = environ.Env(
DEBUG=(bool, False),
MAIL_ENABLED=(bool, False)
)
# False if not in os.environ because of casting above
DEBUG = env('DEBUG') # True
It seems that you are not executing env.py
file before settings.py
.
In local server, you might have executed env.py
file manually and it was recorded in OS. So, even though settings.py
didn't execute the env.py
file, it worked as intended.
In production or remote server Make sure you are importing (or in some other way executing) env.py
before settings.py
.
settings.py
import os
import env # your env.py file
DEBUG = os.environ.get('DEBUG') == 'True'
...
Sidenote
As the top comment mentions env.py
is not the standard/common way of using environment variables, especially in production.
During development using .env
file is very common, and in production, setting environment variables using cloud provider's services as such AWS Secrets Managers etc is the common/recommended way.