1 2 3 4 5 6 7 8 9 10 11 12 | from django import template
from django.conf import settings
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def media_url(value):
"""Searches for {{ MEDIA_URL }} and replaces it with the MEDIA_URL from settings.py"""
return value.replace('{{ MEDIA_URL }}', settings.MEDIA_URL)
media_url.is_safe = True
|
More like this
- Use MEDIA_URL in flatpages with SSL by gobble 2 years, 10 months ago
- Serve static media files from app/media subdirectory by adamlofts 4 years, 9 months ago
- Load static media from secure (SSL) static server (Context Processor) by ianreardon 3 years, 7 months ago
- Tags & filters for rendering search results by exogen 5 years, 1 month ago
- UPDATED: Django Image Thumbnail Filter by danfairs 5 years, 6 months ago
Comments
There's already a context processor that adds {{ MEDIA_URL }} (trunk version only).
If you're not using trunk, why not just write your own context processor like django's? Then you don't need the goofy
{% load media_url %}.#
@kylefox: I think you missed the point of this snippet... since the content comes from the database via flatpage's content field, the interpretation of any Django template code doesn't happen -- it only happens on actual templates themselves. Since this is content being displayed in the template, Django isn't going to run the content through a Template processor.
So all this snippet does is search and replace {{ MEDIA_URL }} in the content with its appropriate setting value. A context processor isn't going to be able to do that.
#
Gotcha.
#