Login

quick_url filter

Author:
tclineks
Posted:
April 28, 2008
Language:
Python
Version:
.96
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. Template tag - list punctuation for a list of items by shapiromatron 5 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 5 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year ago
  5. Help text hyperlinks by sa2812 1 year, 1 month ago

Comments

plisk (on December 27, 2009):

Nice, thanks! :)

#

Please login first before commenting.