【发布时间】:2016-10-27 14:58:25
【问题描述】:
出于多种原因,从单元测试性能到迁移问题 (Django Migration Error: Column does not exist),我发现打开和关闭调试工具栏很有用。
这是我发现的一种控制从环境变量加载它的方法。
不,这不是一个真正的问题,将其视为我希望在 SO 上找到的食谱。
【问题讨论】:
出于多种原因,从单元测试性能到迁移问题 (Django Migration Error: Column does not exist),我发现打开和关闭调试工具栏很有用。
这是我发现的一种控制从环境变量加载它的方法。
不,这不是一个真正的问题,将其视为我希望在 SO 上找到的食谱。
【问题讨论】:
settings.py
INSTALLED_APPS = (
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.messages',
'django.contrib.staticfiles',
#conditionally disable later on
'debug_toolbar',
#...my apps...
)
#disable if not in DEBUG or if $USE_DEBUG_TOOLBAR is not set.
USE_DEBUG_TOOLBAR = bool(int(os.getenv("USE_DEBUG_TOOLBAR", 0))) and DEBUG
#disable as well if running unit tests...
pgm = os.path.basename(sys.argv[0])
if not USE_DEBUG_TOOLBAR or pgm.startswith("test") or pgm.startswith("nosetests"):
li = [app for app in INSTALLED_APPS if not app == "debug_toolbar"]
INSTALLED_APPS = tuple(li)
您的命令行使用可能如下所示:
export USE_DEBUG_TOOLBAR=1 && python manage.py runserver
【讨论】:
我相信放
def show_toolbar(request):
if DEBUG:
return True
DEBUG_TOOLBAR_CONFIG = {
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
}
在 settings.py 中是推荐的方式,也许更简单一些?
【讨论】:
show_toolbar 将由我的环境变量驱动 - 过去我在使用此工具时遇到过很多问题,因此我很少使用它,即使在开发(DEBUG = True)环境。