在Docker中运行django时无法加载css

qjp7pelc  于 2022-11-18  发布在  Go
关注(0)|答案(3)|浏览(267)

我的docker-compose.yml

# pull official base image
FROM python:3.10-alpine

# set work directory
WORKDIR .

# set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1

# install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt .
RUN pip install -r requirements.txt

# copy project
COPY . .

我的Dockerfile如下:

services:
  app:
    build:
      context: .
    ports:
      - "8000:8000"
    command: >
      sh -c "python3 manage.py runserver 0.0.0.0:8000"
  redis:
    image: redis:alpine
  celery:
    restart: always
    build:
      context: .
    command: celery -A search worker -l info
    depends_on:
      - redis
      - app

我的项目目录的树形结构如下:

├── Dockerfile
├── Procfile
├── books
│   ├── __init__.py
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   │   ├── 0001_initial.py
│   │   ├── __init__.py
│   ├── models.py
│   ├── templates
│   │   └── search.html
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── docker-compose.yml
├── manage.py
├── requirements.txt
├── search
│   ├── __init__.py
│   ├── asgi.py
│   ├── celery.py
│   ├── settings.py
│   ├── urls.py
│   └── wsgi.py
└── templates
    ├── base.html
    ├── home.html
    └── registration
        ├── login.html
        └── logout.html

我的设置如下:

ALLOWED_HOSTS = ['0.0.0.0', '127.0.0.1', 'localhost']
DISABLE_COLLECTSTATIC = 0

# Application definition

INSTALLED_APPS = [
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
    "books.apps.BooksConfig",
    "debug_toolbar",
    "corsheaders",
    "django.contrib.postgres",
    "django_celery_beat",
]

MIDDLEWARE = [
    "django.middleware.security.SecurityMiddleware",
    "corsheaders.middleware.CorsMiddleware",
    "django.contrib.sessions.middleware.SessionMiddleware",
    "django.middleware.common.CommonMiddleware",
    "django.middleware.csrf.CsrfViewMiddleware",
    "django.contrib.auth.middleware.AuthenticationMiddleware",
    "django.contrib.messages.middleware.MessageMiddleware",
    "django.middleware.clickjacking.XFrameOptionsMiddleware",
    "debug_toolbar.middleware.DebugToolbarMiddleware",
    "whitenoise.middleware.WhiteNoiseMiddleware",
]
ROOT_URLCONF = "search.urls"
LOGIN_REDIRECT_URL = "home"
LOGOUT_REDIRECT_URL = "home"
CORS_ORIGIN_ALLOW_ALL = False
CORS_ORIGIN_WHITELIST = (
    'http://localhost:8000',
    'https://localhost:8000',
    'http://0.0.0.0:8000',
    'https://0.0.0.0:8000',
    'http://127.0.0.1:8000',
    'https://127.0.0.1:8000'
)
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [BASE_DIR / "templates"],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]

STATIC_URL = "/static/"

如果我访问http://0.0.0.0:8000/admin/,控制台会显示以下内容,并且css不会加载。

The Cross-Origin-Opener-Policy header has been ignored, because the URL's origin was untrustworthy. It was defined either in the final response or a redirect. Please deliver the response using the HTTPS protocol. You can also use the 'localhost' origin instead. See https://www.w3.org/TR/powerful-features/#potentially-trustworthy-origin and https://html.spec.whatwg.org/#the-cross-origin-opener-policy-header.
yeotifhr

yeotifhr1#

您可能需要将SECURE_CROSS_ORIGIN_OPENER_POLICY = None添加到设置文件中。
另外,根据whitenoise和corsheaders文档,检查中间件声明的顺序是否正确。例如:

MIDDLEWARE = [
  ...
  'django.middleware.security.SecurityMiddleware',
  'corsheaders.middleware.CorsMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  ...
]

根据他们的文档,WhiteNoiseMiddleware看起来不应该在列表的底部。

zysjyyx4

zysjyyx42#

settings文件中:
像这样添加以加载static

STATIC_URL = '/static/static/'
STATIC_ROOT = '/vol/web/static'
qybjjes1

qybjjes13#

我不知道这是否是最好的答案,但这是我尝试了很多东西后得到的。
我使用了https://stackoverflow.com/a/39291292
终于成功了

相关问题