Login

Use MEDIA_URL in flatpages

Author:
robhudson
Posted:
March 22, 2008
Language:
Python
Version:
.96
Tags:
filter flatpages
Score:
7 (after 9 ratings)

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 that found in your settings.

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 }}
 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

  1. Use MEDIA_URL in flatpages with SSL by gobble 5 years ago
  2. Breadcrumbs for flatpages by jca 7 years, 6 months ago
  3. UPDATED: Django Image Thumbnail Filter by danfairs 7 years, 8 months ago
  4. YUI editor for flatpages inside admin interface by nnrcschmdt 7 years, 3 months ago
  5. currency filter without using locale by andzep 4 years, 4 months ago

Comments

robhudson (on March 23, 2008):

@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.

#

kylefox (on March 24, 2008):

Gotcha.

#

Please login first before commenting.