How to override Django Admin Templates

Django offers a powerful, built-in admin interface. The admin is a superuser that can overlook, modify, and add or delete data in a database.

To learn how to set up Django admin, visit this shot.

The default admin template offers a simplistic design with the availability of basic admin functions. However, in some cases, a modified admin interface might be required. For this, Django offers a simple feature to override its admin template.

To view the default admin template you can access it in the django/contrib/admin/templates/admin folder. In this situation you will most likely be using a virtual environment and can find this folder in the directory that contains all the installed libraries.

To override these templates, you will need to have an admin folder in your templates folder.

If you do not have a templates folder, you can create it in the main project folder. To do so, you will have to change the project’s settings.py. Find the TEMPLATES section and modify accordingly.

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        // modified here 
        'DIRS': [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',
            ],
        },
    },
]

To override the default template you first need to access the template you want to modify from the django/contrib/admin/templates/admin directory.

Let’s say we want to change base.html – to do so, we would copy that file and paste it into the templates/admin directory in our project. Or, what if we want to make the following changes in base.html in the div with id container? Then, we would add the modifications in the base.html and save.

 <div id="branding">
        {% block branding %}{% endblock %}
        <h1>Hello From Educative</h1>
        </div>

Now, if we run our server and access the admin site, we will see the overridden base template.

widget

You can override other templates in a similar manner. In fact, Django also offers to modify templates per model or per app. For that, in your templates/admin directory you can create directories for each app and then sub-directories in them for the models. However, make sure model names are in lowercase as Django will use the lowercase model name to look for a template.

However, not all templates can be overridden per model or per app, only the templates below can:

  • actions.html
  • app_index.html
  • change_form.html
  • change_form_object_tools.html
  • change_list.html
  • change_list_object_tools.html
  • change_list_results.html
  • date_hierarchy.html
  • delete_confirmation.html
  • object_history.html
  • pagination.html
  • popup_response.html
  • prepopulated_fields_js.html
  • search_form.html
  • submit_line.html

Free Resources