简体   繁体   中英

Django, managing debug flag in settings.py

Say you have

  • base_settings.py
  • debug_settings.py
  • product_settings.py

in debug_settings.py

DEBUG = True
from base_settings import *

in product_settings.py

DEBUG = False
from base_settings import *

in base_settings.py

# this condition check is problamatic, I don't think I can define DEBUG variable outside of `base_settings.py` ?
if DEBUG:
   foo()

How do you have a common base_settings.py which has conditional code dependant on variable (DEBUG) whose value can be changed from the importing module? (from debug_settings, product_settings)

A better way to do this would be to use environment variables

in debug_settings.py:

os.environ["DEBUG"] = "TRUE"

in product_settings.py:

os.environ["DEBUG"] = "FALSE"

then in base_settings.py:

if os.getenv("DEBUG", "TRUE"): #The second argument specifies the default in case "DEBUG" has not been set
    foo()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM