Django Applications not using STATIC_URL

jjmcinto 51 Reputation points
2022-03-22T18:47:53.087+00:00

Just as a proof-of-concept (knowing it is not ideal for security) I published a Django project to Azure with the following in settings.py (with text enclosed in pointy brackets representing actual personal information):

AWS_ACCESS_KEY_ID = '<my_key>'
AWS_SECRET_ACCESS_KEY = '<my_secret_key>'
AWS_STORAGE_BUCKET_NAME = '<my_bucket>'
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'

STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'mysite/static'),
]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

I have confirmed by direct testing that the value for STATIC_URL is being set to 'https://<my AWS storage bucket>.s3.amazonaws.com/static/'.

However, when I inspect an image on my test site at https://<my_app>.azurewebsites.net/ it appears to be trying to find it in 'https://<my_app>.azurewebsites.net/static' (e.g. src="/static/assets/example_image.png").

Note that the code for the image in my view is as follows (with some paraphrasing to anonymize the content):
<img src={% static 'assets/myLogo.png' %} style="width:100px;height:84px;" alt="My Logo">

How do I get the test site to look in my Amazon bucket instead of whatever it is doing now?

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
0 comments No comments
{count} votes

Answer accepted by question author
  1. ajkuma 28,111 Reputation points Microsoft Employee Moderator
    2022-03-24T09:03:37.837+00:00

    jjmcinto-0559, As I understand you’re leveraging AWS storage to service static files on Azure App Service. Thanks for detailed description and sharing the sample code.

    While I’m still checking on this, here are few recommended configurations from App Service standpoint.

    -Oryx attempts to build and run Django apps appropriately, but some configuration is not automated.

    -Add whitenoise to your requirements.txt file.
    (Whitenoise (whitenoise.evans.io) is a Python package that makes it simple for a production Django app to serve its own static files).
    Whitenoise specifically serves those files that are found in the folder specified by the Django STATIC_ROOT variable.

    --In your settings.py file, add the following line for Whitenoise:

    STATICFILES_STORAGE = ('whitenoise.storage.CompressedManifestStaticFilesStorage')

    Also, modify the MIDDLEWARE and INSTALLED_APPS lists to include Whitenoise. -Check the doc section Serve static files for Django apps (below) for more details.

    -Oryx runs manage.py collectstatic on your behalf unless you specify the DISABLE_COLLECTSTATIC env var.

    Try to also set STATIC_ROOT in settings.py:

    // settings.py
    STATIC_ROOT = './static/'

    -To isolate the issue, firstly you may separately test this on Azure.

    -Check this doc section Serve static files for Django apps for more details.

    -App Service - Django Tips


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.