Django app on Azure incorrectly loading static files from an Azure Blob

Viewed 462

This Django app is deployed on Azure as an App Service its static and media files are stored in an Azure storage account - blob.

The project used to work well in the past, but something has changed and now the problem is as following.

Relevant part of the app settings file:

STATIC_URL = 'https://myappstorage.blob.core.windows.net/static/'
MEDIA_URL = 'https://myappstorage.blob.core.windows.net/media/'


STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# any static paths you want to publish
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static')
]

so one would expect that for example a favicon, which is in the root directory of the storage could be found on https://myappstorage.blob.core.windows.net/static/favicon and it is indeed!

But all static files that the app on azure tries to load it tries to load from

https://myappstorage.myappstorage.blob.core.windows.net/static/ (note the duplication of myappstorage), same for media files.

This results in no static files being applied to the page as they are being loaded from the wrong url. When I run the app locally, it works fine. I destroyed it and recreated it, no success. Now I have two copies running, one deployed through FTP and startup command and on using Github action. Still the same problem.

I have also tried little hack-ish workarounds in the setting file, with no success.

1 Answers

Try with adding below in app setting

    DEFAULT_FILE_STORAGE = 'backend.custom_azure.AzureMediaStorage'
    STATICFILES_STORAGE = 'backend.custom_azure.AzureStaticStorage'
    
    STATIC_LOCATION = "static"
    MEDIA_LOCATION = "media"
    
    AZURE_ACCOUNT_NAME = "djangoaccountstorage"
    AZURE_CUSTOM_DOMAIN = f'{AZURE_ACCOUNT_NAME}.blob.core.windows.net'
    STATIC_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{STATIC_LOCATION }/'
    MEDIA_URL = f'https://{AZURE_CUSTOM_DOMAIN}/{MEDIA_LOCATION}

refer this document for more details

Related