Thanks for the direction. I found some examples of where people were using template tags to get the name of a model:
Then, I was able to use the Django documentation to learn about how to create custom template tags and filters:
https://docs.djangoproject.com/en/3.2/howto/custom-template-tags/
Combining those two sources with your answer, helped me create this…
clients/templatetags/client_extra.py
(as the documentation says… don’t forget the init.py file to ensure the directory is treated as a Python package)
from django import template
register = template.Library()
@register.filter
# Gets the name of the passed in field on the passed in object
def verbose_name(the_object, the_field):
return the_object._meta.get_field(the_field).verbose_name
In my template, I am using it like this:
{{ client|verbose_name:"funky_field_1" }}
This is working!!!
So, now I only need to figure out how to, inside the custom filter, determine if a field does NOT have a “verbose_name” present, and then display the field’s “calculated” verbose_name in “sentence case”.
For example, on a field like this in the model, it works fine, and displays the text “Daily living”:
funky_field_1 = models.CharField("Daily living", max_length=20, default='')
On a field like this in the model, it displays the field name, but in lower case, like “funky field 2”:
funky_field_2 = models.CharField(max_length=20, default='')
My “if” statement in my custom filter does not seem to help, because I’m guessing the returned verbose_name is already being calculated when it isn’t present.
@register.filter
# Gets the name of the passed in field on the passed in object
def verbose_name(the_object, the_field):
if the_object._meta.get_field(the_field).verbose_name == "":
return "blankety blank"
else:
return the_object._meta.get_field(the_field).verbose_name
In that example above, I want to see if “blankety blank” will be returned in the funky_field_2 scenario, but it just returns: “funky field 2”.