Opened 6 years ago

Closed 6 years ago

#18254 closed New feature (fixed)

Add support for `as <varname>` to the staticfiles {% static %} template tag.

Reported by: Stephen Burrows Owned by: nobody
Component: contrib.staticfiles Version: master
Severity: Normal Keywords:
Cc: Stephen Burrows Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no


I'm in a situation where I want to prepend the current site's domain to a static file's URL. In olden times, I would've done something like:

{% if STATIC_URL|slice:":4" != "http" %}http://{{ site.domain }}{% endif %}{{ STATIC_URL }}path/to/file.txt

But now the standard way to do things is to use the {% static %} template tag from staticfiles, which currently has no way of assigning to the context - which means that to accomplish the above, you would need to write a custom template tag. Granted, that would be pretty trivial, but it shouldn't be necessary. Having the option of {% static <path> as <varname> %} would parallel nicely with the {% url %} tag and similar.

Change History (3)

comment:1 Changed 6 years ago by Jannis Leidel

Triage Stage: UnreviewedAccepted

Adding an optional as variable seems like a sensible feature request, indeed.

FWIW, as a work around you can use protocol relative URLs, e.g. STATIC_URL = "//" or subclass the default STATICFILES_STORAGE backend, implementing its url.

comment:2 Changed 6 years ago by Andrei Antoukh

I have sent a pull-request with possible solution to this issue. (

comment:3 Changed 6 years ago by Jannis Leidel <jannis@…>

Resolution: fixed
Status: newclosed

In [0a68a2994be17ed2669accead331881cb0be41dd]:

Fixed #18254 -- Added ability to the static template tags to store the result in a contextt variable. Many thanks to Andrei Antoukh for the initial patch.

Note: See TracTickets for help on using tickets.
Back to Top