24

所以我让 docker 和 Django 在本地工作,首先从 Dockerfile 构建镜像,然后使用 Fig 获取 postgres 镜像,将其链接到基础镜像,然后运行本地服务器。一切正常,除了 django_debug_toolbar。由于某种原因,它只是不会出现。在 internal_ips 中也有 dockerhost ip。谁能帮我解决这个问题?Docker 通过 boot2docker 在 mac 上运行。

谢谢!

我的设置:

初始化.py

import os

if 'DEBUG' not in os.environ or not os.environ['DEBUG']:
    from .local import *
else:
    pass

基础.py

""" common and global settings """

from sys import path
from os import environ
from os.path import abspath, basename, dirname, join, normpath
from django.core.exceptions import ImproperlyConfigured
import dj_database_url


def get_env_variable(var_name):
    try:
        return environ[var_name]
    except KeyError:
        error_msg = "Set the environment variable" % var_name
        raise ImproperlyConfigured(error_msg)

# Paths
DJANGO_ROOT = dirname(dirname(abspath(__file__)))
SITE_ROOT = dirname(DJANGO_ROOT)
SITE_NAME = basename(DJANGO_ROOT)
# End Paths


# URLs
MEDIA_ROOT = normpath(join(SITE_ROOT, 'media'))
MEDIA_URL = "/media/"

STATIC_ROOT = normpath(join(SITE_ROOT, 'assets'))
STATIC_URL = "/static/"

STATICFILES_DIRS = (
    normpath(join(SITE_ROOT, 'static')),
)

STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

ROOT_URLCONF = '%s.urls' % SITE_NAME

path.insert(0, join(DJANGO_ROOT, 'apps'))  # add apps folder to system path
# End URLs


# Database
# example:  postgres://joris:luna@localhost/bitbybit
DATABASES = {'default': dj_database_url.config(
   default='postgres://postgres@db:5432/postgres')}
# End Database

# Templates
TEMPLATE_CONTEXT_PROCESSORS = (
    'django.contrib.auth.context_processors.auth',
    'django.core.context_processors.debug',
    'django.core.context_processors.i18n',
    'django.core.context_processors.media',
    'django.core.context_processors.static',
    'django.core.context_processors.tz',
    'django.contrib.messages.context_processors.messages',
    'django.core.context_processors.request',
)

TEMPLATE_LOADERS = (
   'django.template.loaders.filesystem.Loader',
   'django.template.loaders.app_directories.Loader',
)

TEMPLATE_DIRS = (
    normpath(join(SITE_ROOT, 'templates')),
)
# End Templates


# SECURITY WARNING: keep the secret key used in production secret!
# make it unique and store it as an environment variable
SECRET_KEY = r"d%g7_h6cz=xbhs*5-i+e$c7mns*s)^_+#^8n@^-2dno@uie-z9"


# Application
DJANGO_APPS = (
   'django.contrib.admin',
   'django.contrib.auth',
   'django.contrib.contenttypes',
   'django.contrib.sessions',
   'django.contrib.messages',
   'django.contrib.staticfiles',
)

LOCAL_APPS = (
   'home',
)

INSTALLED_APPS = DJANGO_APPS + LOCAL_APPS

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware',
)

WSGI_APPLICATION = '%s.wsgi.application' % SITE_NAME
# End Application


# Internationalization
LANGAUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True
# End Internationalization

本地.py

from .base import *

# Debug config
DEBUG = True
TEMPLATE_DEBUG = DEBUG
# End Debug config

# Hosts
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
# End Hosts


# Django Debug Toolbar config
INSTALLED_APPS += (
    'debug_toolbar', )

INTERNAL_IPS = ('127.0.0.1', 'localhost')
# End Django Debug Toolbar config
4

10 回答 10

48

使用配置 SHOW_TOOLBAR_​​CALLBACK 为我唤醒

def show_toolbar(request):
        return True

DEBUG_TOOLBAR_CONFIG = {
    'SHOW_TOOLBAR_CALLBACK': show_toolbar,
}

我希望这有帮助:)

于 2018-05-14T14:09:28.263 回答
24

如果您想以编程方式执行此操作而不是复制/粘贴您的容器 IP,我建议您喜欢django-cookiecutter人。在您的本地设置文件中:

INTERNAL_IPS = ['127.0.0.1', ]
import socket

# tricks to have debug toolbar when developing with docker
ip = socket.gethostbyname(socket.gethostname())
INTERNAL_IPS += [ip[:-1] + '1']

作为参考,这是django-cookiecutter local.py设置文件的链接。

于 2017-08-10T23:18:07.057 回答
22

您可以制作INTERNAL_IPS一个包含所有内容的对象。这就是我所做的:

if DEBUG:
    # `debug` is only True in templates if the vistor IP is in INTERNAL_IPS.
    INTERNAL_IPS = type(str('c'), (), {'__contains__': lambda *a: True})()

当然,您永远不应该在生产主机上这样做!

解释:

type 函数(三个参数变体:https://docs.python.org/3/library/functions.html#type)创建一个新类,在这种情况下它只有一个__contains__方法(https://docs.python.org /3/reference/datamodel.html#object. contains ) -- contains 用于实现成员资格测试,这意味着运行时会调用此方法,例如“ if ip in INTERNAL_IPS”。如果写成“ def __contains__(self):\n return True”, contains 方法本身可能会更清晰。新创建的类立即实例化(最后的“()”)并分配给 INTERNAL_IPS

于 2018-04-13T13:20:10.967 回答
13

允许我显示Django 调试工具栏的 IP 地址是我的 docker 容器(运行 Django 应用程序的容器)的网关 IP 。

要获取网关的 IP 地址,请运行此命令。

# replace django_container_name with your container name!
docker inspect django_container_name | grep -e '"Gateway"'
# "Gateway": "172.18.0.1",

接下来,将此Getway IP添加到 - 以便settings.py您的配置可能如下所示。

INSTALLED_APPS = (
    ...
    'debug_toolbar',
    ...
)

MIDDLEWARE = [
    ...
    'debug_toolbar.middleware.DebugToolbarMiddleware',
    ...
]

INTERNAL_IPS = ['172.18.0.1']

当然添加__debug__urls.py

import debug_toolbar
from django.conf import settings
from django.urls import include, path

urlpatterns = [
    ...
    path('__debug__/', include(debug_toolbar.urls)),
]

只需遵循djdt 安装指南!

于 2016-09-12T14:26:10.943 回答
3

使用https://unix.stackexchange.com/questions/87468/is-there-an-easy-way-to-programmatically-extract-ip-address上接受的答案,我能够通过传递地址来使其工作主机的 Docker 桥接docker run命令作为环境变量:

-e "DOCKER_HOST=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')"

使用该设置,以下两行settings.py检测到它并允许主机查看工具栏:

if "DOCKER_HOST" in os.environ:
    INTERNAL_IPS = [os.environ["DOCKER_HOST"]]
于 2015-09-17T08:01:00.777 回答
2

解决了。检查请求标头中 REMOTE_ADDR 的值并将其添加到 INTERNAL_IPS。

于 2014-11-13T01:31:54.187 回答
2

正如人们所提到的,您可以使用回调来确定是否应该出现调试工具栏。我将它与通过传入的环境变量一起使用docker-compose

设置.py

def show_toolbar_callback(*args, **kwargs):
    if os.getenv("LOCAL_DEV"):
        return True
    return False


DEBUG_TOOLBAR_CONFIG = {"SHOW_TOOLBAR_CALLBACK": show_toolbar_callback}

docker-compose.yaml

services: 
  django:
    container_name: "django"
    build: "./"
    ports: 
      - "8000:8000"
    volumes:
      - ".:/opt/service"
    environment: 
      LOCAL_DEV: "true"
    depends_on: 
      - "db"
于 2021-03-18T07:01:37.250 回答
1

这是一种不需要将地址注入容器的hacky方式。我不确定如果您的容器连接到多个网络并且如果 docker 更改了它们分配地址的方式可能会中断(尽管这似乎不太可能)。

import subprocess

route = subprocess.Popen(('ip', 'route'), stdout=subprocess.PIPE)
network = subprocess.check_output(
    ('grep', '-Po', 'src \K[\d.]+\.'), stdin=route.stdout).decode().rstrip()
route.wait()
network_gateway = network + '1'
INTERNAL_IPS = [network_gateway]
于 2016-08-16T04:21:29.487 回答
0

1- 在 docker-compose 或 Dockerfile 目录中运行此命令并获取 NETWORK ID:

sudo docker network list

并查看列出所有网络

NETWORK ID     NAME                DRIVER    SCOPE
556e2e7123er   admin_default       bridge    local
aq3033sc09c1   bridge              bridge    local
1983cf1x2cb9   cockroach           bridge    local
0cc1be23b543   flask               bridge    local

2-运行此命令:

ip addr

.
.

8: br-556e2e7123er: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default 
    link/ether **:**:**:**:**:** brd ff:ff:ff:ff:ff:ff
    inet 162.21.0.1/16 brd 172.22.255.255 scope global br-556e2e7123er
       valid_lft forever preferred_lft forever

.
.

3-获取您的IP

NETWORK ID : 556e2e7123er == 8: br-556e2e7123er => ip 162.21.0.1

4-将ip添加到INTERNAL_IPS = [..., 162.21.0.1, ...] ‍‍‍‍‍‍‍‍‍‍</p>

于 2021-07-16T10:28:08.467 回答
0

诀窍是使用gethostbyname_ex而不是gethostbyname方法

INTERNAL_IPS = ['127.0.0.1', ]
if DEBUG:    
   import socket
   hostname, _, ips =socket.gethostbyname_ex(socket.gethostname())
   INTERNAL_IPS += [ip[:-1] + '1' for ip in ips]
于 2022-02-07T19:31:51.960 回答