Opened 14 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)
Change History (15)
comment:1 by , 14 years ago
comment:2 by , 14 years ago
Component: | Uncategorized → django.contrib.staticfiles |
---|---|
Triage Stage: | Unreviewed → Design 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 , 14 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 , 14 years ago
Needs documentation: | set |
---|---|
Needs tests: | set |
Patch needs improvement: | set |
Triage Stage: | Design decision needed → Accepted |
Having given this a bit more thought, I'm convinced this is a useful. There are a few edge cases though:
- 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.
- 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 , 14 years ago
Severity: | → Normal |
---|---|
Type: | → New feature |
comment:6 by , 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.
comment:7 by , 13 years ago
Sorry for the double upload, I just updated the patch to be against the latest trunk.
by , 13 years ago
Attachment: | 15252.3.diff added |
---|
Latest state of the Github repo at https://github.com/jezdez/django/compare/feature/staticfiles-templatetag
comment:8 by , 13 years ago
Needs documentation: | unset |
---|
comment:9 by , 13 years ago
Cc: | added |
---|
by , 13 years ago
Attachment: | 15252.4.diff added |
---|
Forget that before: