1 2 3 4 5 6 7 8 9 10 11 12 | from django.conf import settings
from django.template import Library
def media_path(path):
import urlparse
import os.path
if os.path.exists(os.path.join(settings.MEDIA_ROOT, path)):
return urlparse.urljoin(settings.MEDIA_URL, path)
return ''
register = Library()
register.simple_tag(media_path)
|
More like this
- Use MEDIA_URL in flatpages with SSL by gobble 1 year, 7 months ago
- typygmentdown by ubernostrum 4 years, 5 months ago
- Template tags to integrate with modconcat by matthanger 2 years, 6 months ago
- better paginator template tag by amitu 3 years, 3 months ago
- Django 1.2 template tag {% IF %} with {% ELIF %} support by danilchenko 11 months, 3 weeks ago
Comments
This is now obsolete. You only need to import RequestContext in your views:
from django.templates import RequestContext
And then add a context_instance to your render_to_response, like so:
return render_to_response('main/main.html', {"variable": 0}, context_instance=RequestContext(request))
You can now access the media url anywhere with {{ MEDIA_URL }}
#