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. Hyperlink list filter by lifefloatsby 6 years, 3 months ago
  2. template filter to include protocol and domain in absolute urls by czer 1 year, 9 months ago
  3. Signal to post new saved objects to Twitter by arthurfurlan 5 years, 1 month ago
  4. Display arbitrary models by bjornkri 5 years, 9 months ago
  5. Action that respects the filters in changeview_list by grillermo 1 year, 6 months ago

Comments

plisk (on December 27, 2009):

Nice, thanks! :)

#

(Forgotten your password?)