Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Neat Django admin html decorators
def admin_html_link(short_description=None):
"""Neat Django admin html link decorator
Example model usage:
@util_db.admin_html_link("URL playlist")
def url_playlist_html(self):
return self.url_playlist
or
@util_db.admin_html_link("Parent HifiPlaylistHistory")
def parent_html(self):
if not self.parent:
return None
return self.parent.admin_url_relative, self.parent
"""
def decorator(function):
def wrapper(obj):
result = function(obj)
if not result:
return "N/A"
if isinstance(result, basestring):
url, value = result, result
else:
url, value = result
return '<a href="%s">%s</a>' % (url, value)
wrapper.allow_tags = True
if short_description:
wrapper.short_description = short_description
return wrapper
return decorator
def admin_html_image(short_description=None):
"""Neat Django admin html image decorator
Example model usage:
@util_db.admin_html_image("URL image Hifi")
def url_image_hifi_html(self):
return self.url_image_hifi
"""
def decorator(function):
def wrapper(obj):
url = function(obj)
if not url:
return "N/A"
return '<img src="%s" />' % url
wrapper.allow_tags = True
if short_description:
wrapper.short_description = short_description
return wrapper
return decorator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.