We already knew that django has a beautiful beautiful admin panel. It is adequate for small and medium sized projects. But if we want to change some defaults and add more functionalities in our admin panel, we will need to customize it. Here, In this tutorial we will see some easy but useful customization techniques.
- How to change the ‘Django administration’ text?
- Creating two separate admin sites
- Pagination in Django admin.
- Edit multiple models from one page.
- Make a field uneditable.
- Add Search Functionality
- Add Filter Functionality
How to change ‘Django administration’ text?
By default Django admin shows default ‘site_title‘, ‘site_header‘ and ‘index_title‘. We will replace these defaults. The default values are:
site_title = “Django site adminstration”
site_header = “Django administration”
index_title = “Site administration”
When you go to the login page of django admin you might noticed that the ‘header’ of the login form is ‘Django Administration’.
This can be changed by altering the value of ‘site_header’. We will do it inside our project’s urls.py file.
admin.site.site_header = "Customized Site Name"
Then if you see the title of the site, it is ‘Django site administration’. This the value which is set inside the <title></title> tag of HTML template.
To change this we will have to change the value of “site_title” attribute.
admin.site.site_title = "New Admin Panel"
The next one is ‘index_title‘. It can be seen on the top-left corner of the page. The default value is ‘Site adminstration’
Follow the previous steps to change it’s value.
admin.site.index_title = "Welcome To Admin Panel"
Creating Two Separate Admin Sites
The conventional way is to put all models on a single admin page. Although, it is possible to have several admin sites in a single Django app. Here we will create another admin site and register two models there.
inside our apps ‘admin.py‘ file.
from django.contrib.admin import AdminSite class SecondAdminSite(AdminSite): site_header = "Second Admin Page" site_title = "Admin Portal 2" index_title = "Car & Person Profile Admin" second_admin_site = SecondAdminSite(name='Second Admin') #register models second_admin_site.register(Car) second_admin_site.register(PersonsProfile)
Here we created a new child class of ‘AdminSite’. Then we instantiated the class. After that, we have registered our ‘Car’ & ‘PersonProfile’ model in that admin site. Now, we will have to create an url for that admin site.
from first_app.admin import second_admin_site urlpatterns = [ path('admin/', admin.site.urls), path('second-admin-site/', second_admin_site.urls), ]
Let’s got that URL and see if everything works fine…
Pagination in Django Admin
We have seen how to paginate in HTML pages. If you didn’t visit here. It was a little bit messy. But if we want to add pagination in admin site it’s just a piece of cake. We just need to define the value of ‘list_per_page’ attribute of our model admin.
class PersonAdmin(admin.ModelAdmin): list_display = ['id','full_name','gender','email',] list_per_page = 2
Edit Multiple Models From One Page
To be able to edit multiple objects from one Django admin, we need to use inlines. We have the PersonProfile model, and we need to edit PersonProfile model inside from the Person model edit page.
Firstly, we will create an PersonProfileInline inheriting from TabularInline. Secondly, we will add this in PersonAdmin’s ‘inlines’ list.
class PersonsProfileInline(admin.TabularInline): model = PersonsProfile class PersonAdmin(admin.ModelAdmin): list_display = ['id','full_name','gender','email',] list_per_page = 2 # list inside 'inlines' inlines = [PersonsProfileInline] admin.site.register(Person, PersonAdmin)
How to Make a Field Uneditable?
Even if you are an admin. You don’t have the rights to manipulate users’ information. So, the system shouldn’t let you change the data. The solution is to make those fields uneditable. This is done by listing the fields as ‘readonly_fields’ in the model admin.
class PersonAdmin(admin.ModelAdmin): readonly_fields =['id','first_name','last_name','full_name','gender','email',]
Now the fields are shown as data rather than forms.
Add Search Functionality
Nothing differs from ‘list_display’. Add the fields into ‘search_fields’ attribute.
class PersonAdmin(admin.ModelAdmin, ExportCsvMixin): list_display = ['id','full_name','gender','email',] search_fields = [ 'first_name', 'last_name']
Add Filter Functionality
It’s also easy as pie. Declare an attribute named ‘list_filter’ and add the fields on which basis you want your data to be filtered.
class PersonAdmin(admin.ModelAdmin, ExportCsvMixin): list_filter = ['gender']
Look at the right – top corner of your admin site.
We have seen some useful tricks of customizing admin site. These will save your valuable time and make your admin site more handy and powerful.