33

I have banged my head over this for the last few hours. I can not get {{ MEDIA_URL }} to show up

in settings.py

..
MEDIA_URL = 'http://10.10.0.106/ame/'
..
TEMPLATE_CONTEXT_PROCESSORS = (
  "django.contrib.auth.context_processors.auth",
  "django.core.context_processors.media",
)
..

in my view i have

from django.shortcuts import render_to_response, get_object_or_404
from ame.Question.models import Question

def latest(request):
  Question_latest_ten = Question.objects.all().order_by('pub_date')[:10]
  p = get_object_or_404(Question_latest_ten)
  return render_to_response('Question/latest.html', {'latest': p})

then i have a base.html and Question/latest.html

{% extends 'base.html' %}
<img class="hl" src="{{ MEDIA_URL }}/images/avatar.jpg" /></a>

but MEDIA_URL shows up blank, i thought this is how its suppose to work but maybe I am wrong.

Update Latest version fixes these problems.

Atherion
  • 584
  • 1
  • 5
  • 14

5 Answers5

30

You need to add the RequestContext in your render_to_response for the context processors to be processed.

In your case:

from django.template.context import RequestContext

context = {'latest': p}
render_to_response('Question/latest.html',
                   context_instance=RequestContext(request, context))

From the docs:

context_instance

The context instance to render the template with. By default, the template will be rendered with a Context instance (filled with values from dictionary). If you need to use context processors, render the template with a RequestContext instance instead.

Community
  • 1
  • 1
Sam Dolan
  • 31,966
  • 10
  • 88
  • 84
  • 4
    Awww thank you, I think they need to point that out better for people beginning django. MEDIA_URL is fairly important for design as well as portability. – Atherion Sep 21 '10 at 02:33
25

Adding media template context processor also gets the job done

TEMPLATE_CONTEXT_PROCESSORS = (
    "django.core.context_processors.request",
    "django.contrib.auth.context_processors.auth",
    "django.core.context_processors.media",
    "django.core.context_processors.static",
)
Shrey
  • 569
  • 5
  • 14
  • 1
    Ya this was when it was like 1.2 or 1.3. Your method is the correct way now. Also back then the separation between STATIC and MEDIA were not clear as well. – Atherion Apr 30 '14 at 21:51
  • 1
    This should be the accepted answer. Works like a charm in Django 1.8. – Leistungsabfall Apr 19 '15 at 12:20
  • Yep, thanks. Been tearing my hair out trying to get an image to show over ajax... – Ralph King Apr 24 '15 at 13:24
  • 4
    /!\ Is deprecated, you should use TEMPLATE dict and the option context_processors : https://docs.djangoproject.com/fr/1.9/ref/settings/#media-url – Pwu Jan 11 '16 at 00:17
  • 5
    For the english speakers https://docs.djangoproject.com/en/1.9/ref/settings/#media-url – SudoKid Feb 05 '16 at 13:52
2

You can also use direct_to_template:

from django.views.generic.simple import direct_to_template
...
return direct_to_template(request, 'Question/latest.html', {'latest': p})
leplatrem
  • 1,005
  • 13
  • 25
  • This will do the same thing as `render_to_response('Question/latest.html', context_instance=RequestContext(request, context))`? – Naftuli Kay Sep 06 '11 at 18:45
2

In addition to question provided above can suggest you to take a look at photologue application. It could help you to avoid direct links in template files and use objects instead. F.ex.:

<img src="{{ artist.photo.get_face_photo_url }}" alt="{{ artist.photo.title }}"/>
Volodymyr K.
  • 669
  • 8
  • 20
2

Update: For Django 1.10 users, the both media and static context processors are already moved in django.template from django.core read the following article for more info: https://docs.djangoproject.com/en/1.10/ref/templates/api/#django-template-context-processors-media

jonilyn2730
  • 465
  • 9
  • 21