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?
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.
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=''
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.
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!
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)
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With