1👍
✅
You can definitely embed HTML into the title/th
for admin columns. Instead of using the admin.display decorator
, try it like this:
def get_status(self, obj):
return obj.status
get_status.short_description = mark_safe("<b class='bg-danger'>status</b>")
You can see how it works in tthe source code: https://github.com/django/django/blob/3.2.16/django/contrib/admin/options.py#L860
@staticmethod
def _get_action_description(func, name):
return getattr(func, 'short_description', capfirst(name.replace('_', ' ')))
def _get_base_actions(self):
"""Return the list of actions, prior to any request-based filtering."""
actions = []
base_actions = (self.get_action(action) for action in self.actions or [])
# get_action might have returned None, so filter any of those out.
base_actions = [action for action in base_actions if action]
base_action_names = {name for _, name, _ in base_actions}
# Gather actions from the admin site first
for (name, func) in self.admin_site.actions:
if name in base_action_names:
continue
# RIGHT HERE IS WHERE IT CREATES THE TEXT FOR THE COLUMN TITLE/TH ELEMENT
description = self._get_action_description(func, name)
actions.append((func, name, description))
# Add actions from this ModelAdmin.
actions.extend(base_actions)
return actions
Source:stackexchange.com