How to Customize Django Admin Site?

Hi Dev,
This is a short guide on how to customize django admin site. I explained simply about how to customize django admin. it's simple example of django admin interface customize example. I would like to share with you django admin site customize example tutorial.
The Django framework has a potent management tool called admin. It can instantly add, remove, or change any database model from a web interface right out of the box. However, you may modify the Django admin to enhance your admin capabilities by adding a little extra code.
Here i explained simply step by step example of how to customize django admin site.
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....