Open In App

Django Admin – Redesign and Customization

Last Updated : 26 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Often the default Django admin page is great and it is all we need for our projects but sometimes we may want to expand what default admin interface can do. We have a lot of tools in Django to customize it to our own needs and in this article, we’ll learn how to customize the admin interface and add multiple features:

Let’s setup your project, add models into models.py and register your models.

models.py

Run following commands – 
 

Python manage.py makemigrations
Python manage.py migrate

Now we have created following models, Here is how they look like in Admin panel – 
 

Default Django admin

 

Django Admin – Redesign and Customization

Le’s check how we can add multiple features in Django admin panel. Here is a list of customizations – 
 

1. Changing order of Fields

 

By default, the admin will display fields in the detail view in the same order as defined in the model. But we can change that by making a few edits to the admin.py file without going to models.py and changing the order of different fields.

 

Movie model:
title
length
release_year
title=['release_year', 'title', 'length']
Movie_Model:
release_year
title
length

 

admin.py

 

Python




from django.contrib import admin
from .models import Movie, Customer
 
 
class MovieAdmin(admin.ModelAdmin):
    title = ['release_year', 'title', 'length']
 
 
admin.site.register(Movie, MovieAdmin)
admin.site.register(Customer)


Changed order of fields

2. Adding Search and Filters

Currently, we have only a few entries in our models, but what if the entries increase to hundreds or thousands due to more number of users? To get data of a particular entry will become tedious if we go by looking at each entry. Therefore we need to add a search bar or a filter feature to allow entries to be accessed easily.

Search by the following elements:
search_fields = ['title', 'length', 'release_year']
search_fields = ['first_name', 'last_name', 'phone']
Filter by the following elements:
list_filter = ['release_year']
list_filter = ['last_name']

app_folder / admin.py

Python




from django.contrib import admin
from .models import Movie, Customer
 
 
class MovieAdmin(admin.ModelAdmin):
    # Let you to search with title name, release year and length of duration of movie
    search_fields = ['title', 'length', 'release_year']
    # There will be a filter on release year
    list_filter = ['release_year']
 
 
class CustomerAdmin(admin.ModelAdmin):
    # Let you to search with first name, last name and phone number of the customer
    search_fields = ['first_name', 'last_name', 'phone']
    # There will be a filter on last name
    list_filter = ['last_name']
 
 
admin.site.register(Movie, MovieAdmin)
admin.site.register(Customer, CustomerAdmin)


Movies Model showing Filter and Search

Customers Model showing Search and Filter

3. Viewing Additional Fields

In admin interface, normally we see only one field of our models in the list view. We can add more fields to view with list_display.

list_display=['title', 'release_year']
list_display=['first_name', 'last_name', 'phone']

admin.py

Python




from django.contrib import admin
from .models import Movie, Customer
 
 
class MovieAdmin(admin.ModelAdmin):
  list_display =['title', 'release_year']
   
class customerAdmin(admin.ModelAdmin):
  list_display =['first_name', 'last_name', 'phone']
 
 
admin.site.register(Movie, MovieAdmin)
admin.site.register(Customer, CustomerAdmin)


title and release year Fields in Movie Model

first name, last name and phone number Fields  in Customer Model

4. Editing List View

You can add the ability to edit attribute values directly from the list view instead of going to the detail view

editable_list = ['phone']

admin.py

Python




from django.contrib import admin
from .models import Movie, Customer
 
 
class CustomerAdmin(admin.ModelAdmin):
    editable_list = ['phone']
 
 
admin.site.register(Movie)
admin.site.register(Customer, CustomerAdmin)


Phone Number is editable here in Customer Class

5. Admin Template

Sometimes you may want to change the layout and user interface of admin interface for which you need to add your own templates into the project which will then be reflected to your django admin interface.

Create a folder templates in the root folder and inside it, create another folder admin. Now add your html file inside the admin folder.

templates—->admin—->base_site.html

HTML




{% extends "admin/base.html" %}
 
{% block title %}{% if subtitle %}{{ subtitle }} | {% endif %}{{ title }} | {{ site_title|default:_('Django site admin') }}{% endblock %}
 
{% block branding %}
<h1 id="site-name" style="font-family: cursive;" ><a href="{% url 'admin:index' %}">Video Rental Administration</a></h1>
{% endblock %}
 
{% block nav-global %}{% endblock %}


 
 

Heading is changed 

 

Here, the heading in the admin interface has changed due to the addition of html file. Sometimes template doesn’t get applied to the interface, to avoid that, make sure you’ve done correct template configuration in the settings.py file.

 

import os

TEMPLATES=[

'DIRS': [os.path.join(BASE_DIR, 'templates')],

]

 

This how you can customize the admin template in Django as per your requirements.

 



Similar Reads

Python | Django Admin Interface
In this article, we delve into the capabilities and advantages of the Django Admin Interface, exploring how its customizable features and streamlined workflows empower developers to effortlessly administer their projects, from data management to user interactions. Prerequisites: django-introduction-and-installation | django-introduction-set-2-creat
4 min read
Render Model in Django Admin Interface
Rendering model in admin refers to adding the model to the admin interface so that data can be manipulated easily using admin interface. Django's ORM provides a predefined admin interface that can be used to manipulate data by performing operations such as INSERT, SEARCH, SELECT, CREATE, etc. as in a normal database. To start entering data in your
3 min read
Customize Django Admin Interface
Django admin by default is highly responsive GUI, which offers various features and an overall CRUD application to help developers and users. Moreover, Django admin can be customized to fulfill one's needs such as showing fields on the home page of the table, etc. In this article, we will discuss how to enhance Django-admin Interface. Project struc
3 min read
Securing Django Admin login with OTP (2 Factor Authentication)
Multi factor authentication is one of the most basic principle when adding security for our applications. In this tutorial, we will be adding multi factor authentication using OTP Method. This article is in continuation of Blog CMS Project in Django. Check this out here – Building Blog CMS (Content Management System) with Django Setup 2 Factor Auth
2 min read
Exploring Color Customization in Tkinter
One of the key elements in designing attractive and user-friendly interfaces is the use of Tkinter colors. In this article, we'll discuss the various aspects of using colors in Tkinter applications, covering syntax, code examples, and practical implementations. How to Change Color Tkinter? Tkinter colors refer to the various color options available
6 min read
How to Integrate Flask-Admin and Flask-Login
In order to merge the admin and login pages, we can utilize a short form or any other login method that only requires the username and password. This is known as integrating the admin page and Flask login since it simply redirects users to the admin page when they log in. Let's is how to implement this in this article. Integrate Flask Admin and Fla
8 min read
Adding Tags Using Django-Taggit in Django Project
Django-Taggit is a Django application which is used to add tags to blogs, articles etc. It makes very easy for us to make adding the tags functionality to our django project.Setting up Django Project Installing django-taggit pip install django-taggitADD it to Main Project's settings.py file C/C++ Code INSTALLED_APPS = [ 'django.contrib.admin', 'dja
2 min read
How to customize Django forms using Django Widget Tweaks ?
Django forms are a great feature to create usable forms with just few lines of code. But Django doesn't easily let us edit the form for good designs. Here, we will see one of the ways to customize Django forms, So they will look according to our wish on our HTML page. Basically we will check the methods to include our own custom css, classes, or id
3 min read
Integrating Django with Reactjs using Django REST Framework
In this article, we will learn the process of communicating between the Django Backend and React js frontend using the Django REST Framework. For the sake of a better understanding of the concept, we will be building a Simple Task Manager and go through the primary concepts for this type of integration between React js and Django. Reactjs in a nuts
18 min read
Styling Django Forms with django-crispy-forms
Django by default doesn't provide any Django form styling method due to which it takes a lot of effort and precious time to beautifully style a form. django-crispy-forms solves this problem for us. It will let you control the rendering behavior of your Django forms in a very elegant and DRY way. Modules required:django : django installdjango-crispy
1 min read