Django always capitalizes a model's verbose name in admin index page
Question:
There’s a similar question concerning a field’s verbose_name
: How to stop auto-capitalization of verbose_name in django
When listing available models of an app in the admin index page, Django always capitalizes the first letter of the model’s verbose_name_plural
and use it as the model’s name.
Here’s the code from django.contrib.admin.sites.py:
model_dict = {
'name': capfirst(model._meta.verbose_name_plural),
'perms': perms,
}
But consider the following screenshot, I want to display “vCenters” instead of “VCenters”.
I can remove the capfirst
, and explicitly capitalize other models’ verbose_name_plural
to make it work.
But I have to change django’s source code and it doesn’t seem to be a bug of Django. Are there any better solutions?
Answers:
It’s not that easy…
- Make a copy of the
admin/index.html
template to your
template/admin/index.html
- Create your own template filter:
lowerfirst_if_starts_with_v
in your
own templatetags/my_special_thing.py
directory
@register.filter(is_safe=True)
@stringfilter
def lowerfirst_if_starts_with_v(value):
"""Lowercase the first character of the value."""
return value and value[0] =='v' and value[0].lower() + value[1:]
- Load it in the
index.html
{%load my_special_thing%}
- Apply it to
index.html
on line 23
<th scope="row"><a href="{{ model.admin_url }}">
{{ model.name|lowerfirst_if_starts_with_v }}</a></th>
And done.
There’s a similar question concerning a field’s verbose_name
: How to stop auto-capitalization of verbose_name in django
When listing available models of an app in the admin index page, Django always capitalizes the first letter of the model’s verbose_name_plural
and use it as the model’s name.
Here’s the code from django.contrib.admin.sites.py:
model_dict = {
'name': capfirst(model._meta.verbose_name_plural),
'perms': perms,
}
But consider the following screenshot, I want to display “vCenters” instead of “VCenters”.
I can remove the capfirst
, and explicitly capitalize other models’ verbose_name_plural
to make it work.
But I have to change django’s source code and it doesn’t seem to be a bug of Django. Are there any better solutions?
It’s not that easy…
- Make a copy of the
admin/index.html
template to your
template/admin/index.html
- Create your own template filter:
lowerfirst_if_starts_with_v
in your
owntemplatetags/my_special_thing.py
directory
@register.filter(is_safe=True)
@stringfilter
def lowerfirst_if_starts_with_v(value):
"""Lowercase the first character of the value."""
return value and value[0] =='v' and value[0].lower() + value[1:]
- Load it in the
index.html
{%load my_special_thing%}
- Apply it to
index.html
on line23
<th scope="row"><a href="{{ model.admin_url }}">
{{ model.name|lowerfirst_if_starts_with_v }}</a></th>
And done.