Serving Static files from Azure Storage Account Broken Static Files

I set up an Azure storage account to serve my static files and media, but now all my links from within my templates which are referencing the static path are broken.

From what I can tell it’s missing a / after the {% static '' %} which means the path is now https://#################.blob.core.windows.net/static/mediaassets/plugins/owl-carousel/owl.carousel.css but should be

https://#################.blob.core.windows.net/static/media/assets/plugins/owl-carousel/owl.carousel.css

containing the / before assets/

I’ve literally change every reference to static files within my settings.py but it doesn’t seem to make any difference.

STATIC_URL = '/static/'

STATICFILES_DIRS = [os.path.join(BASE_DIR, "static")]
STATIC_ROOT = os.path.join(BASE_DIR,"static_root")
DEFAULT_FILE_STORAGE = 'app.custom_azure.AzureMediaStorage'
STATICFILES_STORAGE = 'app.custom_azure.AzureStaticStorage'

STATIC_LOCATION = "static"

Within all my templates I have the following syntax <link href="{% static '' %}assets/plugins/gallery/gallery.css" rel="stylesheet">

Other than going through every single template and adding the / before the assets does anyone know how this path is being formed so that i can just make a single change?

That’s not how you use the “static” tag. See the static tag docs for the syntax to use.

This was what was given as part of a theme that i brought, so its in every single html file. Does this mean i have to change every file :frowning:

Only now has it become a problem - it works when serving files locally. If i have to change every file then that’s what I will do. Just hoping that’s not the case

What was in every theme? If it was "{% static '' %}some/dir/file.ext" then you got ripped off and should demand your money back.
If it was just "{% static '' %}", then you should have inserted the some/dir/file.ext in the quotes within the tag.

But yes, you should do that with every instance of the static tag.

How Django manages static files in development is significantly different than how they need to be managed in production. What works in one does not directly correlate to how it will work in the other.

This "{% static '' %}some/dir/file.ext" format is used whenever referencing a file from static. css, js, images everything.

I’ve just followed suit for any additional entry. Suppose i’ll get find and replacing…

I’m still running this locally, but serving static and media from azure storage. Which is now broken. BUT if i use white noise for production would the azure storage account still work do you know?

No clue on that, but the other thing I noticed is that your STATIC_ROOT setting should end with a “/”. (While it is not explicitly stated at Settings | Django documentation | Django to be the case, it’s implied there.) However, I don’t think that’s necessarily the problem, because I’m not consistent across my projects.

Sorry, I don’t use either azure or whitenoise, so I’m not going to be any help there.

No worries. I give it a go. Thanks again for your help.

Once you have (if haven’t already) sorted out any STATIC_ROOT and STORAGE issues, try something along the lines of?:

STATIC_URL = 'https://azure_server_somewhere.azure.domain'
{% load static %}
<img src="{% static 'path/to/your/image.png' %}" alt="">

The way I understand it is that the {% static %} template directive is a wrapper around its argument, and simply prepends it with the STATIC_URL settings value. I may be very wrong about that though, so do no take my word for it – but it does have helped me understand how and why it worked the way it works.

Good luck and do a man sed for all those replaces :wink:

Just in case anyone is looking at serving static and media from a CDN.

I’ve set this up for production and seems to work really well and quick :slight_smile:

Hi @TommyQuality!

What exactly did you set for production? I’m trying to set Azure for static files as well