djangoazure-blob-storagedjango-cors-headers

Some static files can't be loaded because it is blocked by CORS policy (Django) even it is configured based on Django documentation


I faced an issue with a CORS policy and cannot see what is wrong.

I use Django framework and my static files are hosted in Azure Blob

So I am getting errors for these files and it says tuat Access-Control-Allow-Origin is not present: enter image description here enter image description here

What is strange that other files from the same host are loaded.. enter image description here

CORS settings looks like this:

ALLOWED_HOSTS = ['https://support.mdesk.lt', 'https://suppaccountstorage.blob.core.windows.net']
CORS_ORIGIN_ALLOW_ALL = False

CORS_ORIGIN_WHITELIST = (
    'https://support.mdesk.lt',
    'https://suppaccountstorage.blob.core.windows.net',
)

CSRF_TRUSTED_ORIGINS = ['https://support.mdesk.lt']

INSTALLED_APPS = [
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',
    'django.contrib.humanize',
    'bootstrap4form',
    'supportSystem',
    'storages'
]
SITE_ID = 1

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

Wham am I doing wrong?


Solution

  • Loading fonts from a different origin is affected by the CORS settings (your CSS files seem to be fine). Since your assets are not served by the Django application but by the Azure storage you need to adjust the CORS settings there (they will not be processed by the Django middleware you are using). You can change the settings in the Azure Portal.