2

I'm looking for a way to lowercase the first letter of a model in my django admin site.

i.e.: model verbose name is "agent-1.0.0" is shown as "Agent-1.0.0" on the dashboard,

simple but IDK

grappelli trick will also work for me.

django - 1.7.1

also - need this only for one app models group - not all of my dashboard should be lowercase... so, overriding the index.html is not so efficient

Ohad the Lad
  • 1,889
  • 1
  • 15
  • 24

4 Answers4

3

The capitalization is hard-coded in the template, same for the templates in Grappelli.

You can use catavaran's suggestion, but this will transform every model name. Overriding the template is a huge pain in the ass to maintain for something this small.

The only workable solution I can think of is to bypass the capfirst filter with a space:

class Meta:
    verbose_name = " agent-1.0.0"

As capfirst only forcibly capitalizes the first character, nothing will happen if the first character is not a letter.

knbk
  • 52,111
  • 9
  • 124
  • 122
1

Model name passed to template as capfirst(model._meta.verbose_name_plural) so you have to lowercase it in the admin/index.html tempate or via CSS. Imho CSS option is simpler:

div.module tr[class^=model-] th {
    text-transform: lowercase;
}

If you want lowercase only some models (for example User) then change CSS selector to this:

div.module tr.model-user th {
    text-transform: lowercase;
}
Konstantin
  • 24,271
  • 5
  • 48
  • 65
catavaran
  • 44,703
  • 8
  • 98
  • 85
0

With Grapelli you could create a custom Dashboard by running:

 python manage.py customdashboard

and setting GRAPPELLI_INDEX_DASHBOARD on your settings to your custom class.

You can make this custom class extend from the Dashboard class that grappelli offers and override it to your needs. Look especially at the ModelList class, where you can specify the title you want for the model.

cristiano2lopes
  • 3,168
  • 2
  • 18
  • 22
0

There is a CSS-way for those who don't want to override Django admin classes. Override and extend templates/admin/base_site.html template as follows:

{% extends "admin/base_site.html" %}

{% block extrahead %}
    <style>
        h1.model-title {text-transform: lowercase;}
        h1.model-title:first-letter {text-transform: uppercase;}
    </style>
{% endblock %}

{% block content_title %}
    {% if title %}<h1 class="model-title">{{ title }}</h1>{% endif %}
{% endblock %}

This will make only first letter of each content_title uppercase.

You can use the same way to lowercase model name in admin tables as well as sidebar. However, I'd like to point that by tacit agreement model's verbose_name as well as verbose_name_plural shouldn't be capitalized. This will save you a lot of overrides in your project, like I provided above to normalize change_list header.

serghei
  • 3,069
  • 2
  • 30
  • 48