To take advantage of the MEDIA_URL in the file for hosting your static media on a separate server, you need to do a little internal work on your templates and some work to make that setting available to the templates.

Inside a template, where you would normally have:

blah blah <img src="/img/foo.jpg" alt="this is a foo"> blah blah..

You would instead insert {{ MEDIA_URL }} so that it would look like:

blah blah <img src="{{ MEDIA_URL }}/img/foo.jpg" alt="this is a foo"> blah blah..

It generally requires a big search and replace the first time you implement it. After that, seeing other references in the templates has been enough to remind me to use {{ MEDIA_URL }} before any static content.

We could pass {{ MEDIA_URL }} in through each view, but since we'll want it throughout the site, it's often easier to set up a simple context processor to do the heavy lifting.

Note: There is a default media context processor since version 1.0. You can access using {{ MEDIA_URL }} in your templates.



If you are still on django 0.96:

Here's a example snippet from a context process:

from django.conf import settings

def common(request):
    return {'MEDIA_URL': settings.MEDIA_URL}

Then in the under TEMPLATE_CONTEXT_PROCESSORS, we added:


and from there, the MEDIA_URL in became available in all templates as {{ MEDIA_URL }}.

Last modified 13 years ago Last modified on 11/30/09 15:12:35
Back to Top