Skip to content

Instantly share code, notes, and snippets.

@jmorakuebler
Created November 13, 2019 13:11
Show Gist options
  • Save jmorakuebler/6a58d92b9a01ad91a581f7dfd6772b2d to your computer and use it in GitHub Desktop.
Save jmorakuebler/6a58d92b9a01ad91a581f7dfd6772b2d to your computer and use it in GitHub Desktop.
Custom django template tag that returns the verbose name of a field.
from django.template import Library
register = Library()
@register.simple_tag
def get_field_verbose_name(instance, field_name):
"""Returns the verbose_name of the specified field."""
return instance._meta.get_field(field_name).verbose_name.title()
{% load get_field_verbose_name %}
<!-- Will display the verbose_name and the value of the field -->
<p>{% get_field_verbose_name object 'my_field' %}: {{ object.my_field }}</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment