- April 23, 2007
- template tag media simple_tag
- 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
- Serialize a model instance by chriswedgwood 1 week, 5 days ago
- Automatically setup raw_id_fields ForeignKey & OneToOneField by agusmakmun 9 months, 2 weeks ago
- Crispy Form by sourabhsinha396 10 months, 1 week ago
- ReadOnlySelect by mkoistinen 10 months, 2 weeks ago
- Verify events sent to your webhook endpoints by santos22 11 months, 2 weeks ago