Opened 12 years ago

Closed 12 years ago

Last modified 12 years ago

#4105 closed (wontfix)

New tag to generate URLs for media files

Reported by: Amr Mostafa Owned by: Adrian Holovaty
Component: Template system Version: master
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


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)

media_tag.diff (1.6 KB) - added by Amr Mostafa 12 years ago.
simple_media_tag.patch (2.2 KB) - added by Chris Beaven 12 years ago.
Simpler take

Download all attachments as: .zip

Change History (10)

Changed 12 years ago by Amr Mostafa

Attachment: media_tag.diff added

comment:1 Changed 12 years ago by…

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.

comment:2 Changed 12 years ago by…

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 Changed 12 years ago by Amr Mostafa

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 Changed 12 years ago by Simon G. <dev@…>

Resolution: wontfix
Status: newclosed
Summary: [Patch] New tag to generate URLs for media filesNew tag to generate URLs for media files
Triage Stage: UnreviewedDesign 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.

Changed 12 years ago by Chris Beaven

Attachment: simple_media_tag.patch added

Simpler take

comment:6 Changed 12 years ago by Chris Beaven

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 Changed 12 years ago by Marc Fargas <telenieko@…>

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 Changed 12 years ago by Marc Fargas <telenieko@…>

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.

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