Opened 13 years ago

Closed 12 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: dev
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

Description

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 by Jannis Leidel, 13 years ago

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 = "//www.example.com/" or subclass the default STATICFILES_STORAGE backend, implementing its url.

comment:2 by Andrei Antoukh, 13 years ago

I have sent a pull-request with possible solution to this issue. (https://github.com/django/django/pull/66)

comment:3 by Jannis Leidel <jannis@…>, 12 years ago

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