Login

quick_url filter

Author:
tclineks
Posted:
April 28, 2008
Language:
Python
Version:
.96
Tags:
filter get_absolute_url quick_url
Score:
1 (after 1 ratings)

A simple filter that generates a url from an object that has a get_absolute_url method.

{{ object|quick_url }}

a previous, simpler approach: #511

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

Comments

plisk (on December 27, 2009):

Nice, thanks! :)

#

Please login first before commenting.