views:

114

answers:

3

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.

+3  A: 

You need to add the RequestContext in your render_to_repsonse 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.

sdolan
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.
Scottix
You're welcome! I believe this bit me too when I first started. If you'd like to help out people like yourself in the future, I'd look at the book/documentation and find out where you think this should be mentioned. Create a patch and submit a ticket @ djangoproject.com. Also, if indicate that this issue answered your question, click the checkmark to the left of the answer. See http://stackoverflow.com/faq
sdolan
A: 

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})
emel
A: 

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 }}"/>
vkjr