Alter Django admin change list title text

Question:

I’m creating some custom views for the Django admin interface that use the standard change-list as an interim stage. This works fine, apart from the fact the change-list page H1 is ‘Select object to change’. ‘Change’ is not the right verb for the action the user will be undertaking in my custom views.

I have found the django.contrib.admin templates that control the layout of the change-list pages (change_list.html and change_list_results.html) but I cannot find where the title is supplied from. I’m guessing it is a variable passed in by a view someplace?

How can I override this text with something less misleading e.g. ‘Select object’ instead of ‘Select object to change’? I’m OK with changing it across all the change-list views, not just the particular ones I’m trying to customise; but I’d prefer a solution that is an override as opposed to a modification of the django.contrib.admin code if possible.

Update: I have found the view responsible for the change list, it’s main.py in djangocontribadminviews. The variable is self.title on line 69 (Django 1.0). I have acheived the result I am looking for by editing this line

self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s to change') % force_unicode(self.opts.verbose_name))

to read

self.title = (self.is_popup and ugettext('Select %s') % force_unicode(self.opts.verbose_name) or ugettext('Select %s') % force_unicode(self.opts.verbose_name))

I’d still be really interested to hear a better way of achieving the same result that doesn’t involve hacking the django.contrib.admin code – it looks like there already is an option to have the title the way I’d like it, but I’m not sure how to trigger that?

Asked By: Jen Z

||

Answers:

There is already ticket for ChangeList customization: http://code.djangoproject.com/ticket/9749. This will give the ability to change many additional aspects of admin application. Unfortunately there is no clean way to achieve your goals.

Answered By: zgoda

For current versions of Django:

class CustomChangeList(django.contrib.admin.views.main.ChangeList):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title = 'My Cool Title'

class MyAdmin(ModelAdmin):
    def get_changelist(self, request, **kwargs):
        return CustomChangeList
Answered By: Josh Kelley

Not sure if still relevant, but another way to do this would be passing the extra_content for the changelist_view method. For ex:

from django.contrib import admin

class MyCustomAdmin(admin.ModelAdmin):

    def changelist_view(self, request, extra_context=None):
        extra_context = {'title': 'Change this for a custom title.'}
        return super(MyCustomAdmin, self).changelist_view(request, extra_context=extra_context)
Answered By: jarussi

You can override the method and pass it the title in extra_content, see:

def change_view(self, request, object_id, form_url='', extra_context=None):

        extra_context = {'title': 'Hello Title'}

        return super(BlogAdmin, self).change_view(request, object_id,
            form_url, extra_context=extra_context)
Answered By: gutierri

As of Django 3.1.7

I think the OP is asking about the changelist "content" title (the one shown on the page below breadcrumbs, not in the browser tab title). Django sets it from the model’s verbose_name_plural (set in model class’ class Meta). If it is not explicitly set, Django uses the model class name with ‘s’ suffixed. Here is the code from Django admin change_list.html:

<!-- CONTENT-TITLE -->
{% block content_title %}
    <h1>{{ cl.opts.verbose_name_plural|capfirst }}</h1>
{% endblock %}

So if just setting the verbose_name_plural does not suffice/work for you, consider overriding the change_list.html template and do your thing in the {% block content_title %}. If it is too complicated to do in the template, you can pass your own context data to the admin template as given in the SO answer here:
Django how to pass custom variables to context to use in custom admin template?

Answered By: amolbk

You can change the title on "Select … to change" page with "extra_context" in "changelist_view()" as shown below:

# "admin.py"

from django.contrib import admin
from .models import Person

@admin.register(Person)
class PersonAdmin(admin.ModelAdmin):
    def changelist_view(self, request, extra_context=None):
        extra_context = {'title': 'This is a custom title.'} # Here
        return super().changelist_view(request, extra_context=extra_context)

This is how the title on "Select Person to change" page looks like as shown below:

enter image description here

Answered By: Kai – Kazuya Ito