[英]Django, managing debug flag in settings.py
Say you have说你有
in debug_settings.py在 debug_settings.py
DEBUG = True
from base_settings import *
in product_settings.py在 product_settings.py
DEBUG = False
from base_settings import *
in base_settings.py在 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?您如何拥有一个通用的 base_settings.py,它的条件代码取决于变量(DEBUG),其值可以从导入模块中更改? (from debug_settings, product_settings)
(来自 debug_settings、product_settings)
A better way to do this would be to use environment variables更好的方法是使用环境变量
in debug_settings.py:在 debug_settings.py 中:
os.environ["DEBUG"] = "TRUE"
in product_settings.py:在 product_settings.py 中:
os.environ["DEBUG"] = "FALSE"
then in base_settings.py:然后在 base_settings.py 中:
if os.getenv("DEBUG", "TRUE"): #The second argument specifies the default in case "DEBUG" has not been set
foo()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.