python - Django ignoring DEBUG value when I use os.environ? - Stack Overflow

admin2025-04-17  20

In my Django settings I have:

in env.py:

os.environ.setdefault(
    "DEBUG", "True"
    )

in settings.py: DEBUG = os.environ.get('DEBUG') == 'True'

However some of the systems are not visible after deploying but they are perfect on my local server.

I tried changing the DEBUG value to false but nothing changes.

In my Django settings I have:

in env.py:

os.environ.setdefault(
    "DEBUG", "True"
    )

in settings.py: DEBUG = os.environ.get('DEBUG') == 'True'

However some of the systems are not visible after deploying but they are perfect on my local server.

I tried changing the DEBUG value to false but nothing changes.

Share Improve this question edited Feb 1 at 4:36 Pat Myron 4,6582 gold badges27 silver badges51 bronze badges asked Jan 31 at 21:54 Tafadzwa Mgcini MangenaTafadzwa Mgcini Mangena 11 bronze badge 2
  • 3 What is env.py? That doesn't look like a standard Django module. How are you using it? – John Gordon Commented Jan 31 at 21:58
  • "However some of the systems are not visible after deploying"? – willeM_ Van Onsem Commented Feb 2 at 10:25
Add a comment  | 

2 Answers 2

Reset to default 0

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.

转载请注明原文地址:http://anycun.com/QandA/1744845226a88421.html