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 , 13 years ago
Triage Stage: | Unreviewed → Accepted |
---|
comment:2 by , 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 , 12 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
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 defaultSTATICFILES_STORAGE
backend, implementing itsurl
.