Django Admin - Redesign and Customization
Last Updated :
26 Nov, 2021
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 fields2. 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 Filter3. 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 Model4. 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 Class5. 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
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 ho
3 min read
Custom Django Management Commands
Prerequisites:Â Django Introduction and Installation Manage.py in Django is a command-line utility that works similar to the django-admin command. The difference is that it points towards the project's settings.py file. This manage.py utility provides various commands that you must have while workin
4 min read
Customizing Phone Number Authentication in Django
We will implement phone number-based authentication in Django by creating a custom user model that uses phone numbers instead of usernames for login. Youâll learn how to build a custom user manager, update settings, and integrate the new user model with the Django admin panel to enhance security and
2 min read
Creating Custom Decorator in Django for different permissions
Decorators are incredibly helpful tools that allow you to dynamically change the functionality of a function, method, or class without having to utilize subclasses directly or modify the function's source code. The decorator's login_required, require HTTP methods, require POST, and others are needed
8 min read
Build a To-Do application Using Django, React and Tailwind
This article will guide you in creating a To-Do application using React and Tailwind with the Django Framework. Weâll explore the integration of Django, React, and Tailwind, and go through the step-by-step process of implementing the To-Do application. What is a To-Do application?A To-Do application
6 min read
Django Sign Up and login with confirmation Email | Python
Django provides a built-in authentication system that handles users, login, logout, and registration. In this article, we will implement a user registration and login system with email confirmation using Django and django-crispy-forms for elegant form rendering.Install crispy forms using the termina
7 min read
Django - Creating apps | Set - 2
In the previous article, we discussed why apps are important in Django project management? What are the benefits of using Django apps? In this article, we will discuss what are we going to build and how do apps play a vital role in Django projects? Project outline - We will build a localhost e-comme
2 min read
How to Create an App in Django ?
In Django, an app is a web application that performs a specific functionality, such as blog posts, user authentication or comments. A single Django project can consist of multiple apps, each designed to handle a particular task. Each app is a modular and reusable component that includes everything n
3 min read
Django - Creating Apps | Set - 1
Prerequisites: Django â Dealing with warnings Why do we need apps? In Django Set 2 (Creating a Project), we saw how we can display text in our browser using Django but that is not the best and pythonic way. Django recommends using the project-app relationship to build Django projects. Any website co
1 min read
Built in & Custom Model Managers in Django
Django manager is a class that acts as an interface through which Django models interact with databases. Every model has at least one manager object. It has a lot of methods, attributes to ease working with databases. In fact, many beginner-level Django developers do not know that they use the Manag
6 min read