Andy McKay

Apr 14, 2007

Django media-url

Django applications have this thing that is very useful called MEDIA_URL specified in the configuration, that allows you to have a pointer to your static files, your css, js, images and so on. This is nice because it basically assumes that Django won't be responsible for serving your static stuff out to the world. Let Apache, IIS or whatever do that.

So in your templates, you can link to the MEDIA_URL to point to those files. On my development laptop it would be say localhost on my production server But wait the MEDIA_URL setting isn't available in a template, through a tag or anything. You have to change your views to use RequestContext and use a context preprocessor. If you do this at a beginning of a project its about 5 lines of code as detailed here.

So repeatedly people keep opening bugs on this eg: and and I can understand not having a template tag, or having all the settings exposed. But this is a useful tag, designed for this purpose, but hard to use. Why? There's no real explanation:

"Im marking this as a wontfix due to the slippery slope potential. If we added this, then people would want a context processor for another setting, and for another setting, and for another. We've already ruled out the idea of a "settings" context processor (see [1278]), so I'd recommend that people write their own two-line context processors if they really want this stuff."

People do seem to want it a lot, so it's being added to SimpleTemplate, the advantage of being able to do my own stuff. If there is another way people change the links of their static files, please let me know.