Django: Ticket #4105: New tag to generate URLs for media fileshttps://code.djangoproject.com/ticket/4105
<p>
I believe a tag to generate URLs for media files is going to be handy and natural to use. Example usage would be:
</p>
<pre class="wiki">&lt;link href="{% media 'css/mystyle.css' %}" rel="stylesheet" type="text/css" /&gt;
</pre><p>
If css/mystyle doesn't exist, empty string <em> is returned.
</em></p>
en-usDjangohttps://www.djangoproject.com/s/img/site/hdr_logo.gifhttps://code.djangoproject.com/ticket/4105
Trac 1.2.2Amr MostafaSat, 21 Apr 2007 16:27:25 GMTattachment sethttps://code.djangoproject.com/ticket/4105
https://code.djangoproject.com/ticket/4105
<ul>
<li><strong>attachment</strong>
set to <em>media_tag.diff</em>
</li>
</ul>
Ticketnick.lane.au@…Mon, 23 Apr 2007 01:36:22 GMThttps://code.djangoproject.com/ticket/4105#comment:1
https://code.djangoproject.com/ticket/4105#comment:1
<p>
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:
</p>
<p>
<code>&lt;link href="{{ MEDIA_URL }}css/mystyle.css" rel="stylesheet" type="text/css" /&gt;</code>
</p>
<p>
See the patch on ticket <a class="closed ticket" href="https://code.djangoproject.com/ticket/1278" title="#1278: enhancement: MEDIA_URL needs to be accessible from templates (closed: fixed)">#1278</a>, or James Bennett's <a class="ext-link" href="http://www.b-list.org/weblog/2007/04/12/django-template-utilities"><span class="icon">​</span>template_utils</a> for an example of this.
</p>
<p>
Note: Since this is dependent on having a RequestContext for the context processors to be applied, I also have have a <code>{% media_url %</code>} template tag to accomplish the same thing in templates that aren't able to use a RequestContext (e.g. emails).
</p>
<p>
Note 2: On a somewhat related sidenote, I also have <code>{% css %</code>} and <code>{% javascript %</code>} 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.
</p>
Ticketnick.lane.au@…Mon, 23 Apr 2007 01:42:11 GMThttps://code.djangoproject.com/ticket/4105#comment:2
https://code.djangoproject.com/ticket/4105#comment:2
<p>
Oops I somehow neglected to notice the attached patch, so ignore the links re: context processors :-)
</p>
<p>
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.
</p>
TicketAmr MostafaMon, 23 Apr 2007 09:59:05 GMThttps://code.djangoproject.com/ticket/4105#comment:3
https://code.djangoproject.com/ticket/4105#comment:3
<p>
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.
</p>
<p>
I thought the existence check is useful because:
</p>
<ul><li>It will save your server from the 404 hits (I assume user would also have other resources than CSS/Javascript, e.g. images).
</li><li>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.
</li></ul><p>
Thanks again, I'd love to get more feedback!
</p>
TicketSimon G. <dev@…>Wed, 25 Apr 2007 11:26:36 GMTstatus, stage, summary changed; resolution sethttps://code.djangoproject.com/ticket/4105#comment:4
https://code.djangoproject.com/ticket/4105#comment:4
<ul>
<li><strong>status</strong>
changed from <em>new</em> to <em>closed</em>
</li>
<li><strong>stage</strong>
changed from <em>Unreviewed</em> to <em>Design decision needed</em>
</li>
<li><strong>resolution</strong>
set to <em>wontfix</em>
</li>
<li><strong>summary</strong>
changed from <em>[Patch] New tag to generate URLs for media files</em> to <em>New tag to generate URLs for media files</em>
</li>
</ul>
<p>
this is a duplicate of <a class="closed ticket" href="https://code.djangoproject.com/ticket/1278" title="#1278: enhancement: MEDIA_URL needs to be accessible from templates (closed: fixed)">#1278</a>, <a class="closed ticket" href="https://code.djangoproject.com/ticket/3818" title="#3818: Expose chosen settings to default context processor. (closed: wontfix)">#3818</a>, <a class="closed ticket" href="https://code.djangoproject.com/ticket/2532" title="#2532: enhancement: [patch] made media_url accessibly thru context_processor (closed: wontfix)">#2532</a> which have all been marked as wontfix. However, given the frequency of this request, I'll raise it on #django-developers to see if core's still convinced on this.
</p>
TicketMarc Fargas <telenieko@…>Thu, 03 May 2007 14:44:18 GMThttps://code.djangoproject.com/ticket/4105#comment:5
https://code.djangoproject.com/ticket/4105#comment:5
<p>
Discussion on <a class="ext-link" href="http://groups.google.com/group/django-developers/browse_thread/thread/8547935c231edf44/#"><span class="icon">​</span>http://groups.google.com/group/django-developers/browse_thread/thread/8547935c231edf44/#</a>
</p>
TicketChris BeavenThu, 24 May 2007 03:29:34 GMTattachment sethttps://code.djangoproject.com/ticket/4105
https://code.djangoproject.com/ticket/4105
<ul>
<li><strong>attachment</strong>
set to <em>simple_media_tag.patch</em>
</li>
</ul>
<p>
Simpler take
</p>
TicketChris BeavenThu, 24 May 2007 03:42:55 GMThttps://code.djangoproject.com/ticket/4105#comment:6
https://code.djangoproject.com/ticket/4105#comment:6
<p>
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).
</p>
<p>
So here's a simpler take using the <code>simple_tag</code> decorator and appropriately documented.
</p>
<p>
IMO this is the best solution of the bunch (of closed tickets) since it doesn't introduce the requirement for templates to use <code>RequestContext</code>.
</p>
TicketMarc Fargas <telenieko@…>Thu, 24 May 2007 07:58:44 GMThttps://code.djangoproject.com/ticket/4105#comment:7
https://code.djangoproject.com/ticket/4105#comment:7
<p>
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?
</p>
TicketMarc Fargas <telenieko@…>Tue, 29 May 2007 11:29:30 GMThttps://code.djangoproject.com/ticket/4105#comment:8
https://code.djangoproject.com/ticket/4105#comment:8
<p>
On the latest patch, atleast my python says it's urlparse.urljoin() not urlparse.join()
</p>
<p>
Anyway, this ticket is being closed in favour of <a class="closed ticket" href="https://code.djangoproject.com/ticket/1278" title="#1278: enhancement: MEDIA_URL needs to be accessible from templates (closed: fixed)">#1278</a> as per <a class="ext-link" href="http://groups.google.com/group/django-developers/browse_thread/thread/8547935c231edf44/#"><span class="icon">​</span>recent discussion</a> on django-developers.
</p>
Ticket