How to Customize Django Admin Site?

Hi Dev,
In this comprehensive tutorial, we’ll learn how to customize the Django admin site with a simple step-by-step approach. Whether you want to enhance your Django admin panel UI or just learn how to add new features like images, list views, or headers, this guide has you covered.
The Django framework comes with a powerful admin tool. But did you know you can customize the Django admin interface to suit your project needs better? Let's dive into how you can make your admin dashboard look professional and functional.
Step 1: Create a Project
In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:
django-admin startproject example
Step 2: Create a App
Now we'll create a single app called core to store a list of post names. We're keeping things intentionally basic. Stop the local server with Control+c and use the startapp command to create this new app.
python manage.py startapp core
Step 3: Update setting.py
In this step we require to do two things in our settings.py file, One is our installed app name Add the below lines to your settings.py file:
Next, you need to add it in the settings.py file as follows:
example/settings.py.... INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'core', ]
Step 4: Create a Model
In this step we will require the database model for storing contacts.Open the core/models.py file and add the following code:
core/models.pyfrom django.db import models from django.db.models.deletion import CASCADE class Category(models.Model): cat_romance = "Romance" cat_fantacy = "Fantacy" cat_thriller= "Thriller" cat_horror = "Horror" cat_crime = "Crime" cat_true_story= "True Story" category = models.CharField( max_length=100, choices=( (cat_crime, cat_crime), (cat_fantacy, cat_fantacy), (cat_horror, cat_horror), (cat_romance, cat_romance), (cat_thriller, cat_thriller), (cat_true_story, cat_true_story) ) ) def __str__(self): return self.category class Publisher(models.Model): publisher_name = models.CharField(max_length=100) publish_date = models.DateField def __str__(self): return self.publisher_name class Author(models.Model): gender_male = "Male" gender_female = "Female" gender_other = "Other" name = models.CharField(max_length=100) gender = models.CharField(max_length=100, choices=( (gender_female, gender_female), (gender_male, gender_male), (gender_other, gender_other) ) ) country = models.CharField(max_length=100) def __str__(self): return self.name class Details(models.Model): book_name = models.CharField(max_length=100) category = models.ForeignKey(Category, on_delete=CASCADE) pages = models.IntegerField(default=1) publisher = models.ForeignKey(Publisher, on_delete=models.CASCADE) Author = models.ForeignKey(Author, on_delete=CASCADE) def __str__(self): return self.book_name
Ok, all set. We can engender a migrations file for this change, then integrate it to our database via migrate.
python manage.py makemigrations python manage.py migrate
Step 5: Update admin.py
core/admin.pyfrom django.db import models from .models import Category, Publisher ,Details, Author class categoryAdmin(admin.ModelAdmin): pass class publisherAdmin(admin.ModelAdmin): pass class detailsAdmin(admin.ModelAdmin): pass class authorAdmin(admin.ModelAdmin): pass admin.site.register(Category, categoryAdmin) admin.site.register(Publisher, publisherAdmin) admin.site.register(Details, detailsAdmin) admin.site.register(Author, authorAdmin)
Step 6: Run the Server
In this step, we’ll run the local development server for playing with our app without deploying it to the web.
python manage.py runserver
You can now log in using the superuser credentials at http://127.0.0.1:8000/admin/.
Now, customize the Django Admin Site...
- Setting plural text for models
- Changing the Django administration header text
- Removing the default groups
- Using list_display
- Adding an image to Django admin
Setting plural text for models
By adding the following code to models.py, we can change how the models category and information appear on the admin site.
class Category(models.Model): # ... class Meta: #new verbose_name_plural = "Categories" class Details(models.Model): # ... class Meta: #new verbose_name_plural = "Details"
Changing the Django administration header text
Instead, add the following code to urls.py to update the admin site header text, login page, and HTML title tag for our bookshop.
admin.site.site_header = "Custom bookstore admin" admin.site.site_title = "Custom bookstore admin site" admin.site.index_title = "Custom Bookstore Admin"
The Django administration text that shows on the login page and the admin site is modified by the site header. Every admin page's title text is modified by the site title.
Removing the default groups
Consider the scenario where we want to remove the Groups app that comes with our Django admin site by default.
We will go ahead and import it then unregister it in admin.py.
from django.contrib.auth.models import Group # new #... admin.site.unregister(Group) # new
If you like, you could also go ahead and unregister users through the same process.
Using list_display
Additionally, you might want more than one column to be visible on the change list page for your details model. We must modify admin.py in order to add additional fields.
class detailsAdmin(admin.ModelAdmin): list_display=('book_name','category','Author','pages','publisher') #pass
Adding an image to Django admin
Using list display, we might also wish to include the author's picture in addition to their name. We must first install a third-party programme named pillow in order to accomplish that.
pip install pillow
Then, open settings.py and add the following code. This code tells Django where to store the images.
import os # at the top #... MEDIA_URL = '/media/' MEDIA_ROOT = os.path.join(BASE_DIR , 'media')
Now let’s create the media folder and add an images folder inside it.
mkdir media\images
Then, open urls.py and add the code below to add our media folder to the static files.
# below the other imports from . import settings from django.contrib.staticfiles.urls import static from django.contrib.staticfiles.urls import staticfiles_urlpatterns #... urlpatterns +=staticfiles_urlpatterns() urlpatterns +=static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
We will import mark safe into models.py and also add the picture field to our Author model. Then, we'll include a feature that lets us view the image on our admin site.
In this step, we need to create the views for performing the fetch record to the database.Open the core/views.py file and add:
# at the top from django.utils.safestring import mark_safe # in our Author model class Author(models.Model): # ..... author_pic = models.ImageField(upload_to='images/', null=True) def image_tag(self): return mark_safe('<img src="/../../media/%s" width="150" height="150" />' % (self.author_pic)) image_tag.allow_tags = True
Now let us make migrations then migrate to reflect the changes in our database.
python manage.py makemigrations python manage.py migrate
Finally, we’ll call our function inside the list display. Let’s go to our admin.py and modify the authorAdmin().
class authorAdmin(admin.ModelAdmin): list_display=['name','image_tag']
I Hope It will help you....
❓ Frequently Asked Questions (FAQ)
- 1. How do I change the Django admin site title and header?
- You can update it in your
urls.py
file usingadmin.site.site_header
,site_title
, andindex_title
. - 2. How do I remove default models like Groups from Django admin?
- Use
admin.site.unregister(Group)
after importing the Group model fromdjango.contrib.auth.models
. - 3. Can I display images in Django admin list views?
- Yes, use the
ImageField
in your model and display it with a custom method usingmark_safe
. - 4. What is the purpose of list_display in Django admin?
list_display
defines the fields you want to show in the admin panel’s list view of your model records.- 5. Is it possible to customize Django admin without using third-party packages?
- Absolutely! Django’s built-in admin is very extensible through ModelAdmin, templates, and configuration options.
📝 Final Thoughts:
Customizing your Django admin site not only improves usability but also adds a professional touch to your application. This tutorial gives you everything you need to personalize your admin interface. Whether you're showcasing books, users, or products, Django admin can be molded to your needs with just a few lines of code.
👍 I hope this tutorial helps you enhance your Django admin site effectively!