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
- get_object_or_none by azwdevops 1 month, 4 weeks ago
- Mask sensitive data from logger by agusmakmun 3 months, 3 weeks ago
- Template tag - list punctuation for a list of items by shapiromatron 1 year, 6 months ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 6 months ago
- Serializer factory with Django Rest Framework by julio 2 years, 1 month ago
Comments
Nice, thanks! :)
#
Please login first before commenting.