Code

Opened 2 years ago

Closed 22 months ago

#18254 closed New feature (fixed)

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

Reported by: melinath Owned by: nobody
Component: contrib.staticfiles Version: master
Severity: Normal Keywords:
Cc: melinath 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.

Attachments (0)

Change History (3)

comment:1 Changed 2 years ago by jezdez

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

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 Changed 2 years ago by niwi

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

comment:3 Changed 22 months ago by Jannis Leidel <jannis@…>

  • Resolution set to fixed
  • Status changed from new to closed

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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.