Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django {{ MEDIA_URL }} blank @DEPRECATED

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.

like image 411
Atherion Avatar asked Sep 21 '10 02:09

Atherion


2 Answers

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.

like image 120
Sam Dolan Avatar answered Sep 28 '22 02:09

Sam Dolan


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",
)
like image 43
Shrey Avatar answered Sep 28 '22 01:09

Shrey