#4105 closed (wontfix)
New tag to generate URLs for media files
Reported by: | Amr Mostafa | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Template system | Version: | dev |
Severity: | Keywords: | ||
Cc: | amr.mostafa@… | Triage Stage: | Design decision needed |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I believe a tag to generate URLs for media files is going to be handy and natural to use. Example usage would be:
<link href="{% media 'css/mystyle.css' %}" rel="stylesheet" type="text/css" />
If css/mystyle doesn't exist, empty string is returned.
Attachments (2)
Change History (10)
by , 18 years ago
Attachment: | media_tag.diff added |
---|
comment:1 by , 18 years ago
comment:2 by , 18 years ago
Oops I somehow neglected to notice the attached patch, so ignore the links re: context processors :-)
I don't know if it's necessary to check for the existence of the media file though, as I would assume you'd only be trying to include files that you know exist.
comment:3 by , 18 years ago
Thanks for taking a look ;-), It's perfectly possible to do this in the userland (with Django, what isn't anyway ;-) but I believe that this's something normally expected from Django itself, and is needed by almost everybody. Implementation as a tag is more natural to the user IMHO.
I thought the existence check is useful because:
- It will save your server from the 404 hits (I assume user would also have other resources than CSS/Javascript, e.g. images).
- It's easier to spot the empty strings than it's to spot typos when you view-source your template when you suspect something is wrong.
Thanks again, I'd love to get more feedback!
comment:4 by , 18 years ago
Resolution: | → wontfix |
---|---|
Status: | new → closed |
Summary: | [Patch] New tag to generate URLs for media files → New tag to generate URLs for media files |
Triage Stage: | Unreviewed → Design decision needed |
comment:5 by , 18 years ago
comment:6 by , 17 years ago
I really liked Amr's method, but I'm not convinced that it should check for the file first (you might be doing some tricky redirect or something in Apache or the like - we shouldn't be getting in the way of this).
So here's a simpler take using the simple_tag
decorator and appropriately documented.
IMO this is the best solution of the bunch (of closed tickets) since it doesn't introduce the requirement for templates to use RequestContext
.
comment:7 by , 17 years ago
I'm also against the checking of the file first, it got me mad when I missed one media file as I got no 404 errors :)
Does that need some kind of test or can we check that it or bring it to the list for discussion?
comment:8 by , 17 years ago
On the latest patch, atleast my python says it's urlparse.urljoin() not urlparse.join()
Anyway, this ticket is being closed in favour of #1278 as per recent discussion on django-developers.
For this sort of thing I just use a context processor to add the MEDIA_URL setting to my templates. The code would then become:
<link href="{{ MEDIA_URL }}css/mystyle.css" rel="stylesheet" type="text/css" />
See the patch on ticket #1278, or James Bennett's template_utils for an example of this.
Note: Since this is dependent on having a RequestContext for the context processors to be applied, I also have have a
{% media_url %
} template tag to accomplish the same thing in templates that aren't able to use a RequestContext (e.g. emails).Note 2: On a somewhat related sidenote, I also have
{% css %
} and{% javascript %
} tags that I use to do css/js imports automatically that take template inheritance into account, so I can add additional imports to child templates. This is kind of a specialist case though, and outside the scope of the Django core.