1

I have a Django app which uses ManifestStaticFilesStorage, in production static assets are served via a CDN and references to them in templates are correctly set, e.g.

Template:

{% load staticfiles %}
<script type="text/javascript" src="{% static 'some.js' %}"></script>

Rendered HTML:

     <script type="text/javascript" src="https://example.com/some.abc123.js"></script>

Is it possible to get the fully-qualified URL to a static asset in a view? I would like to have a view that redirects to a static asset on my CDN.

Something like:

import django.http HttpResponseRedirect

def my_lovely_view(request):
    static_asset_url = static('some.js') # does this fn exist?
    # static_asset_url == 'https://example.com/some.abc123.js'
    return HttpResponseRedirect(static_asset_url)

1 Answers1

1
from django.contrib.staticfiles.templatetags.staticfiles import static

static_asset_url = static('x.jpg')

try this

Exprator
  • 26,992
  • 6
  • 47
  • 59