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. template filter to include protocol and domain in absolute urls by czer 10 months, 2 weeks ago
  2. DateTimeWidget using JSCal2 by ramusus 3 years, 10 months ago
  3. Create short URL redirects for site urls. by matt.geek.nz 4 years, 3 months ago
  4. Hyperlink list filter by lifefloatsby 5 years, 4 months ago
  5. Image model with thumbnail by clawlor 2 years, 10 months ago

Comments

plisk (on December 27, 2009):

Nice, thanks! :)

#

(Forgotten your password?)