quick_url filter

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from django import template
from django.utils.html import conditional_escape
from django.utils.safestring import mark_safe

register = template.Library()

def quick_url(addressable_object, autoescape=None):
    if not hasattr(addressable_object, 'get_absolute_url'):
        return addressable_object
    if autoescape:
        esc = conditional_escape
    else:
        esc = lambda x: x
    result = '<a href="%s">%s</a>' % (esc(addressable_object.get_absolute_url()), esc(addressable_object))
    return mark_safe(result)

quick_url.needs_autoescape = True
register.filter(quick_url)

More like this

  1. Link filter by sean2000 4 years, 5 months ago
  2. Django Breadcrumbs Snippet by flashingpumpkin 3 years ago
  3. Absolute URL Templatetag by johnboxall 3 years ago
  4. Automatic CRUD urls from your models... by codigodaniel 1 year, 4 months ago
  5. Hyperlink list filter by lifefloatsby 4 years, 4 months ago

Comments

plisk (on December 27, 2009):

Nice, thanks! :)

#

(Forgotten your password?)