tags:

views:

101

answers:

2

Hi,

like exposed here, one can set a MEDIA_URL in settings.py (for example i'm pointing to Amazon S3) and serve the files in the view via {{ MEDIA_URL }}. Since MEDIA_URL is not automatically in the context, one have to manually add it to the context, so, for example, the following works:

#views.py

from django.shortcuts import render_to_response
from django.template import RequestContext

def test(request):
    return render_to_response('test.html', {}, context_instance=RequestContext(request))

This means that in each view.py file i have to add from django.template import RequestContext and in each response i have to explicitly specify context_instance=RequestContext(request).

Is there a way to automatically (DRY) add MEDIA_URL to the default context? Thanks in advance.

+3  A: 

There is a generic view for this use :

direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs)

It is not well documented (in my opinion : it doesn't tell that it uses a RequestContext), so I advise you to check out the implementation :

http://code.djangoproject.com/browser/django/trunk/django/views/generic/simple.py

I think it is what you are looking for ...

sebpiq
I use this thing instead of render_to_template
Kugel
thanks, that is what i was looking for. it should be more evident in the documentation!
pistacchio
A: 

Add "django.core.context_processors.media" to your TEMPLATE_CONTEXT_PROCESSORS in the settings file.

ionut bizau