When a template is rendered in template tag code, MEDIA_URL is not in context

Posted by culebrón on Stack Overflow See other posts from Stack Overflow or by culebrón
Published on 2010-04-24T20:47:03Z Indexed on 2010/04/24 20:53 UTC
Read the original article Hit count: 225

Filed under:
|

I want to use a template for 2 template tags. In the template, I used {{ MEDIA_URL }} and discovered that MEDIA_URL is not in context as expected. Had to use get_config and pass it manually.

Why is the setting not in context, how else can I put it there, or maybe there's a better way that a template tag? (include, etc?)

from django.template import Library
from apps.annoying.functions import get_config
from django.template.loader import render_to_string
register = Library()

@register.simple_tag
def next_in_gallery(photo, gallery):
    next = photo.get_next_in_gallery(gallery)
    return make_arrow('right', next)

@register.simple_tag
def previous_in_gallery(photo, gallery):
    prev = photo.get_previous_in_gallery(gallery)
    return make_arrow('left', prev)

def make_arrow(direction, object):
    return render_to_string('myapp/arrow.html',
        {'direction': direction, 'object': object,
        'MEDIA_URL': get_config('MEDIA_URL', '')})

© Stack Overflow or respective owner

Related posts about django

Related posts about django-templates