Customising the Admin Template

Learn about the basics of adding a custom template to your Django admin page.

Customising admin templates

To override or replace Django admin templates, you first need to configure your project to tell Django where to find your custom templates. To do so, you need to define the path in the DIRS option of the TEMPLATES parameter, which is located in helloworld/settings.py.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [
            BASE_DIR,
            os.path.join(BASE_DIR, 'templates')
        ],
        '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',
            ],
        },
    },
]

templates` as specified as a directory name, so you need to create this directory in your project as well.

You have created the necessary directory tree using the following commands:

mkdir sample_app/templates
mkdir sample_app/templates/admin
mkdir sample_app/templates/admin/sample_app

Django expects the directory to be organized in the following pattern so that it can find the custom templates:

{{app}}/templates/admin/{{app}}/

And if you want to override a template only for a specific model, you need to create a subdirectory named after our model.

{{app}}/templates/admin/{{app}}/{{model}}/

mkdir sample_app/templates/admin/sample_app/author

Please note that the admin app will put the model name in lowercase when looking for the directory, so make sure to name the directory in lowercase especially if you are going to run your app on a case-sensitive filesystem.

To override an admin template, you will need to copy and edit the template from the django/contrib/admin/templates/admin directory and save it to one of the directories you just created.

For example, if you want to add a change to the change list view for all the models in your app named sample_app, you will copy contrib/admin/templates/admin/change_list.html to the templates/admin/sample_app/ directory and then edit the HTML file to make the change. This has already been done for you. See the changes in the code widget at the end of the lesson.

cp ../djangoadminenv/lib/python3.7/site-packages/django/contrib/admin/templates/admin/change_list backoffice/templates/admin/sample_app/

If you want to make a change to the change list view for only your specific model named Author, you will copy that same file to the templates/admin/sample_app/author directory of your project.

cp ../djangoadminenv/lib/python3.7/site-packages/django/contrib/admin/templates/admin/change_list sample_app/templates/admin/sample_app/author/

Now, you will edit your global change_list.html file, which has been placed in sample_app/templates/admin/sample_app for you. Find the {% block content %} in this file and add an <h2> in it with a custom message like this:

...
{% block content %}
<h2> Global Change </h2>
<div id="content-main">
...

If you browse through the change list page of any of your models, you will see your h2 header with the text Global Change.

Get hands-on with 1200+ tech skills courses.