Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter Django admin change list title text

I am 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 django\contrib\admin\views. 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?

like image 410
Jen Z Avatar asked Aug 21 '09 09:08

Jen Z


People also ask

How do I change the admin heading in Django?

To change the admin site header text, login page, and the HTML title tag of our bookstore's instead, add the following code in urls.py . The site_header changes the Django administration text which appears on the login page and the admin site. The site_title changes the text added to the <title> of every admin page.

How do I change app label in Django admin?

Rename the folder which is in your project root. Change any references to your app in their dependencies, i.e. the app's views, the urls.py and settings.py files. Edit the database table django_content_type with the following command: UPDATE django_content_type SET app_label='' WHERE app_label=''

Can we change Django admin theme?

To do so, you will have to change the project's settings.py . Find the TEMPLATES section and modify accordingly. To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

How can I remove extra's from Django admin panel?

You can add another class called Meta in your model to specify plural display name. For example, if the model's name is Category , the admin displays Categorys , but by adding the Meta class, we can change it to Categories . Literally saved my life!


3 Answers

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)
like image 109
jarussi Avatar answered Sep 24 '22 02:09

jarussi


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
like image 39
Josh Kelley Avatar answered Sep 23 '22 02:09

Josh Kelley


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.

like image 25
zgoda Avatar answered Sep 23 '22 02:09

zgoda