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.

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.

[Patch] New tag to generate URLs for media files →
New tag to generate URLs for media files

Triage Stage:

Unreviewed →
Design decision needed

this is a duplicate of #1278, #3818, #2532 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.

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.

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?