当我将 Django 项目移动到 docker compose 时,调试设置被忽略
debug setting is ignored when I move my django project to docker compose
我们有一个 Django 应用程序。现在我们 运行 它在 Vagrant 上使用 Ubuntu 18.04。一切正常。现在我们决定转向 docker 撰写。我们已经设置好一切,一切都很好 运行ning。唯一的问题是 debug
值有一个非常奇怪的错误。
因此,我们的设置文件中有 DEBUG=True
。我们将该值传递给配置模板以使用来包含在 html 中。为此,我们有以下设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
os.path.join(PROJECT_DIR, 'templates'),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
# List of callables that know how to import templates from various sources.
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.request',
'leads.context_processors.leads',
]
}
},
]
如您所见,我们声明了选项debug
,其值为DEBUG
。我在那里的两个环境都设置了断点,我可以看到 DEBUG=True
.
现在有趣的部分来了,如果我在 django 模板上设置断点,例如,这个:
{% if not debug %}
<link rel="stylesheet" href="somlink" />
{% else %}
<link rel="stylesheet" href="somelink.css" />
{% endif %}
然后我们发现两种不同的行为:
- Vagrant app:
debug
, 正如预期的那样,设置为 True,我可以在 Pycharm. 的调试器中看到 debug
的值
- Docker compose:
debug
cannot be seen in the debugger (I suppose that for some reason, it's not defined).
有趣的是,我在设置文件上设置了一个断点,以检查是否出于某种原因,DEBUG
设置在 [=45= 中被设置为 False
] 撰写,但它设置为 True
!!
我什至不知道如何尝试解决这个问题,因为我无法理解问题出在哪里。有任何想法吗?也许 pycharm 有问题,我应该向他们开票吗?
Debug context_processor 仅当 Request.META["REMOTE_ADDR"] 在 INTERNAL_IPS 中时才有效。 Docker 创建自己的网络。而且你的ip会不一样。 Official django docs about debug context processor
我们有一个 Django 应用程序。现在我们 运行 它在 Vagrant 上使用 Ubuntu 18.04。一切正常。现在我们决定转向 docker 撰写。我们已经设置好一切,一切都很好 运行ning。唯一的问题是 debug
值有一个非常奇怪的错误。
因此,我们的设置文件中有 DEBUG=True
。我们将该值传递给配置模板以使用来包含在 html 中。为此,我们有以下设置:
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
os.path.join(PROJECT_DIR, 'templates'),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
# List of callables that know how to import templates from various sources.
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
'django.template.context_processors.debug',
'django.template.context_processors.i18n',
'django.template.context_processors.media',
'django.template.context_processors.static',
'django.template.context_processors.request',
'leads.context_processors.leads',
]
}
},
]
如您所见,我们声明了选项debug
,其值为DEBUG
。我在那里的两个环境都设置了断点,我可以看到 DEBUG=True
.
现在有趣的部分来了,如果我在 django 模板上设置断点,例如,这个:
{% if not debug %}
<link rel="stylesheet" href="somlink" />
{% else %}
<link rel="stylesheet" href="somelink.css" />
{% endif %}
然后我们发现两种不同的行为:
- Vagrant app:
debug
, 正如预期的那样,设置为 True,我可以在 Pycharm. 的调试器中看到 - Docker compose:
debug
cannot be seen in the debugger (I suppose that for some reason, it's not defined).
debug
的值
有趣的是,我在设置文件上设置了一个断点,以检查是否出于某种原因,DEBUG
设置在 [=45= 中被设置为 False
] 撰写,但它设置为 True
!!
我什至不知道如何尝试解决这个问题,因为我无法理解问题出在哪里。有任何想法吗?也许 pycharm 有问题,我应该向他们开票吗?
Debug context_processor 仅当 Request.META["REMOTE_ADDR"] 在 INTERNAL_IPS 中时才有效。 Docker 创建自己的网络。而且你的ip会不一样。 Official django docs about debug context processor