evansd / whitenoise

Radically simplified static file serving for Python web apps
https://whitenoise.readthedocs.io
MIT License
2.51k stars 148 forks source link

Django app on Azure: STORAGES (previously STATICFILES_STORAGE) WhiteNoise variable breaks application #554

Open NickMol opened 8 months ago

NickMol commented 8 months ago

Python Version

3.8.10

Django Version

4.2.5

Package Version

6.6.0

Description

I am deploying a Django app on Microsoft Azure App Services and I used Whitenoise for static file handling. My application breaks when I start using the STORAGES variable in my production settings. When I comment this setting out it works perfectly.

I have tried several things:

Detailed error for the ManifestCompressedStatisFilesStorage is: Missing staticfiles manifest entry for 'admin/css/base.css'

You can find my full code here: https://github.com/NickMol/Django-React-Deploy-Tutorial/tree/main/backend My deployment settings (deployment.py) are listed down below:

import os
from .settings import * 
from .settings import BASE_DIR

ALLOWED_HOSTS = [os.environ['WEBSITE_HOSTNAME']]
CSRF_TRUSTED_ORIGINS = ['https://' + os.environ['WEBSITE_HOSTNAME']]
DEBUG = False
SECRET_KEY = os.environ['MY_SECRET_KEY']

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    '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',
]

# CORS_ALLOWED_ORIGINS = [
# ]

STORAGES = {
    "default": {
        "BACKEND": "django.core.files.storage.FileSystemStorage",
    },
    "staticfiles": {
        "BACKEND": "whitenoise.storage.CompressedManifestStaticFilesStorage",
    },
}

#String that does work: "BACKEND": "django.contrib.staticfiles.storage.StaticFilesStorage"

CONNECTION = os.environ['AZURE_POSTGRESQL_CONNECTIONSTRING']
CONNECTION_STR = {pair.split('=')[0]:pair.split('=')[1] for pair in CONNECTION.split(' ')}

DATABASES = {
    "default": {
        "ENGINE": "django.db.backends.postgresql",
        "NAME": CONNECTION_STR['dbname'],
        "HOST": CONNECTION_STR['host'],
        "USER": CONNECTION_STR['user'],
        "PASSWORD": CONNECTION_STR['password'],
    }
}

STATIC_ROOT = BASE_DIR/'staticfiles'
adamchainz commented 7 months ago

Detailed error for the ManifestCompressedStatisFilesStorage is: Missing staticfiles manifest entry for 'admin/css/base.css'

This error is from the part of Django that Whitenoise extends, ManifestStaticFilesStorage. It happens when referencing a file that doesn't exist.

Make sure you are running collectstatic on every deploy.