Opened 13 years ago

Closed 13 years ago

#15252 closed New feature (fixed)

get_static_url in static templatetags

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

Description

Hello,

When using the settings STATICFILES_STORAGE, it's the storage that should return the url.

For example, a tag of this type works very well:

@register.simple_tag()
def get_static_url(filepath):
    if storage.exists(filepath):
        return storage.url(filepath)
    return None

and so STATIC_URL is not required anymore

Attachments (5)

15252.1.diff (2.3 KB ) - added by Jannis Leidel 13 years ago.
initial patch
15252.2.diff (12.5 KB ) - added by Jannis Leidel 13 years ago.
Adds working hashed file storage backend
15252.2.2.diff (11.8 KB ) - added by Jannis Leidel 13 years ago.
Adds working hashed file storage backend
15252.3.diff (40.3 KB ) - added by Jannis Leidel 13 years ago.
Latest state of the Github repo at https://github.com/jezdez/django/compare/feature/staticfiles-templatetag
15252.4.diff (60.3 KB ) - added by Jannis Leidel 13 years ago.

Download all attachments as: .zip

Change History (15)

comment:1 by ohardy, 13 years ago

Forget that before:

from django import template
from django.core.files.storage import get_storage_class

register = template.Library()

storage = get_storage_class(settings.STATICFILES_STORAGE)()

comment:2 by Jannis Leidel, 13 years ago

Component: Uncategorizeddjango.contrib.staticfiles
Triage Stage: UnreviewedDesign decision needed

I'm not sure how that would be an improvement to the get_static_prefix template tag, to be honest. Would you mind to elaborate?

http://docs.djangoproject.com/en/dev/ref/contrib/staticfiles/#the-get-static-prefix-templatetag

comment:3 by ohardy, 13 years ago

Several differences:

  • STATIC_URL is not required since the storage is constructing the URL
  • If a storage wants to build a URL encoded, for example, it is impossible to build this URL with STATIC_URL.
  • STATIC_URL been given duplicate such that for S3 storage as we already set the URL when you declare storage.

All these remarks also apply to MEDIA_URL

comment:4 by Jannis Leidel, 13 years ago

Needs documentation: set
Needs tests: set
Patch needs improvement: set
Triage Stage: Design decision neededAccepted

Having given this a bit more thought, I'm convinced this is a useful. There are a few edge cases though:

  1. The docs need to carefully explain how the storage influences the result of this template tag, e.g. signed URLs, increased latency with remote storage backends etc. It's usually also a pretty advanced topic, so we need to make sure this won't confuse beginners.
  1. FileFields and ImageFields can be passed different storage backends, which is why the template tag doesn't completely apply to MEDIA_URL. It should probably check whether it's passed a file instance or a string, e.g.
@register.simple_tag()
def get_media_url(file_or_path):
    if isinstance(file_or_path, UploadedFile):
        return file_or_path.url
    return storage.url(file_or_path)

comment:5 by Łukasz Rekucki, 13 years ago

Severity: Normal
Type: New feature

by Jannis Leidel, 13 years ago

Attachment: 15252.1.diff added

initial patch

comment:6 by Jannis Leidel, 13 years ago

Easy pickings: set
Has patch: set
Needs tests: unset
Patch needs improvement: unset
UI/UX: unset

The attached patch implements this idea solely for staticfiles since I don't believe it's really needed for MEDIA_URL (and the file storage backends) -- the filefields are capable of returning the url themeselves.

by Jannis Leidel, 13 years ago

Attachment: 15252.2.diff added

Adds working hashed file storage backend

by Jannis Leidel, 13 years ago

Attachment: 15252.2.2.diff added

Adds working hashed file storage backend

comment:7 by Jannis Leidel, 13 years ago

Sorry for the double upload, I just updated the patch to be against the latest trunk.

by Jannis Leidel, 13 years ago

Attachment: 15252.3.diff added

comment:8 by Jannis Leidel, 13 years ago

Needs documentation: unset

comment:9 by Chris Streeter, 13 years ago

Cc: Chris Streeter added

by Jannis Leidel, 13 years ago

Attachment: 15252.4.diff added

comment:10 by Jannis Leidel, 13 years ago

Resolution: fixed
Status: newclosed

In [16594]:

Fixed #15252 -- Added static template tag and CachedStaticFilesStorage to staticfiles contrib app.

Many thanks to Florian Apolloner and Jacob Kaplan-Moss for reviewing and eagle eyeing.

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