How do I change the text of "Add [Model-Name]" button in Django Admin Interface?

Hi there,

When we login to Django Admin Interface as a superuser, we see the list of models on the left sidebar. When we click on any model name, we go to the list display page of that model which have 'Add [Model-Name]" button on uuper right corner. How do I change the text/value of that button? In my case, I have User Model, and I want to change the “Add User” text on list display page of User Model to “Invite User”. How do I accomplish that? I have encircled the button with red in the screenshot attached.

image

I have tried different solutions told in this stackoverflow question and in this django documentation. But I am unable to achieve. I tried to override change_form.html by changing {% blocktranslate with name=opts.verbose_name %}Add {{ name }}{% endblocktranslate %} to {% blocktranslate with name=opts.verbose_name %}Invite {{ name }}{% endblocktranslate %}. I put the overriden file change_form.html in pricingmeister/accounts/templates/admin/. But i could not see the change.

The hierarchy of my Django Project and folders is below:

Below is my settings.py (truncated some code to show only relevant part of code

.
.
.
INSTALLED_APPS = [
    # Local Apps
    "pricingmeister.accounts",
    "pricingmeister.core",
    # Django Apps
    "django.contrib.admin",
    "django.contrib.auth",
    "django.contrib.contenttypes",
    "django.contrib.sessions",
    "django.contrib.messages",
    "django.contrib.staticfiles",
]
.
.
.
TEMPLATES = [
    {
        "BACKEND": "django.template.backends.django.DjangoTemplates",
        "DIRS": [],
        "APP_DIRS": True,
        "OPTIONS": {
            "context_processors": [
                "django.template.context_processors.debug",
                "django.template.context_processors.request",
                "django.contrib.auth.context_processors.auth",
                "django.contrib.messages.context_processors.messages",
            ],
        },
    },
]
.
.
.

What wrong am I doing? Any help will be appreciated.

That’s the wrong template to override. The list view for a model is not the change view for an instance of that model, it’s change_list.html. But, more specifically, the template being used for that button is change_list_object_tools.html.

@KenWhitesell thank you for the answer. I have now one more problem. I want to change the form after we click on the ‘Add User’ button, or actually ‘Invite User’ button as it is this way in my case. How can I change that form? Also, how can I change the behave after I submit that form?

It’s all documented at The Django admin site | Django documentation | Django

@KenWhitesell I tried to follow the guidelines mentioned in the link you provided. To be honest, I got successful too, but the problem is that the custom behavior I made is being applied in “Change” Page as well, whereas I only need that custom behavior in 'Add" Page.

The custom behavior I want is, when I click on “Add User”, actually “Invite User” in my case, I want a custom form to be displayed not the Django’s default add/create form, then upon submission of that form, I want to send an invitation to the User instead of saving the Model. I achieved this by creating a following custom from:

# forms.py

class InvitationForm(forms.ModelForm):
    display_name = forms.CharField(max_length=254)
    workspace = forms.ChoiceField(choices=Workspace.objects.all())
    is_admin = forms.BooleanField()

    class Meta:
        model = User
        fields = ("display_name", "email", "workspace", "is_admin")

NOTE: the email field in above code is an attribute of User Model.

Then I added this form in UserAdmin as below:

# admin.py

class UserAdmin(BaseUserAdmin):
    form = forms.InvitationForm
    fields = ("display_name", "email", "workspace", "is_admin")
.
.
.

and then I did override the method save_model to perform invitation functionality instead of saving/creating User model instance. It works, but it also works for “Change” page/behavior. For example, when I send invitation to the user using this process via email, and after the user accepts invitation, their account is created. Now, later if I want to update/change User details, when I click on the user from list display page, I see the same custom form which I created for Invite behavior, whereas I want to see the Django’s default form where I can change User’s email, name, password, permissions, groups etc.

How can I make it possible? Any help will be appreciated.

I would suggest you link your “Invite User” to a custom view with your specific form and not try to fit this into the admin. You could then have your “success_url” send you back to the admin.

@KenWhitesell actually this is what I want as a default behavior for superuser. I don’t want any way for superuser to add a user without sending an invitation, and after user accepts the invitation and user account is created, then I want the superuser to be able to access the details of user and update/change it.

A superuser bypasses all authorization tests within the admin. You’d have to do some custom coding of your own to prevent this from happening.

I’m going to reinforce this paragraph from the Django docs on the admin:

The admin has many hooks for customization, but beware of trying to use those hooks exclusively. If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views.