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
- Template tag - list punctuation for a list of items by shapiromatron 11 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 11 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 6 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 7 months ago
- Help text hyperlinks by sa2812 1 year, 8 months ago
Comments
Nice, thanks! :)
#
Please login first before commenting.