- Author:
- ubernostrum
- Posted:
- April 23, 2007
- Language:
- Python
- Version:
- .96
- Score:
- 9 (after 9 ratings)
A slightly improved version of snippet #195 which keeps the logic but makes use of the simple_tag
decorator to drastically simplify the code.
For an alternative to this sort of tag, check out the media context processor in my template_utils app.
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
- Template tag - list punctuation for a list of items by shapiromatron 10 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 5 months ago
- Help text hyperlinks by sa2812 1 year, 6 months 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 }}
#
Please login first before commenting.