This is a copy of snippet 654, modified to allow dynamic MEDIA_URL, as you might need that for SSL in combination with snippet 1754.
This is a template filter to enable the use of the MEDIA_URL setting in content from the flatpages database table. It searches for {{ MEDIA_URL }} and replaces it with the current MEDIA_URL added by a context processor.
Note: To set up, drop the above code into a file called media_url.py in your templatetags directory in one of your INSTALLED_APPS, and add the filter to your flatpages template like so:
{% load media_url %} {{ flatpage.content|media_url:MEDIA_URL }}
1 2 3 4 5 6 7 8 9 10 11 | from django import template
from django.template.defaultfilters import stringfilter
register = template.Library()
@register.filter
@stringfilter
def media_url(value,MEDIA_URL):
"""Searches for {{ MEDIA_URL }} and replaces it with the current MEDIA_URL"""
return value.replace('{{ MEDIA_URL }}', MEDIA_URL)
media_url.is_safe = True
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 9 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 9 months, 1 week ago
- Serializer factory with Django Rest Framework by julio 1 year, 4 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 4 months ago
- Help text hyperlinks by sa2812 1 year, 5 months ago
Comments
Please login first before commenting.