Home > Software design >  Unable to load css while running django in docker
Unable to load css while running django in docker

Time:11-11

If I access http://0.0.0.0:8000/admin/

The console says the following and the css does not load.

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.

My settings is as follows:

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/"

CodePudding user response:

You may need to add SECURE_CROSS_ORIGIN_OPENER_POLICY = None to your settings file.

Also, check to see that your middleware is declared in the correct order according to the whitenoise and corsheaders docs. For example:

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

The WhiteNoiseMiddleware doesn't look like it should be at the bottom of the list, according to their docs.

CodePudding user response:

In your settings file:

Add like this to load static.

STATIC_URL = '/static/static/'
STATIC_ROOT = '/vol/web/static'
  • Related