Building a Full Stack Application is challenging and fun. By building the full stack application we get to learn so many things like database, routing, template, etc. But there the one most important thing we get to learn is managing data with the help of an admin panel or admin interface. Coding for the Admin interface is quite time consuming and take a lot of efforts. also while coding for the admin interface we need to take care of securities and permissions. we would require to write middleware and validation.
Nowadays technologies growing rapidly and come with out of the box features which makes the developers life easier. so Django also comes with a feature Admin Interface to make developers life a lot easier.
So, In this Django tutorial series we will discuss the Django admin interface
- What is Django Admin Interface
- How to access the Admin interface
- How To register model to the admin interface
- How to unregister model from the admin interface
- Customizing Django Admin interface
What is Django Admin Interface?
The admin interface is a built-in module in Django which is design for executing admin related operation that includes reading, writing, updating and deleting. from this interface, the admin can manage users and permissions related to users.
The exciting thing about this interface is that it is highly customizable. we can customize this as we want
How to access the Admin interface?
When we create a Django project the admin interface is by default included in `INSTALLED_APPS`
To access This admin interface on the browser write `/admin` at `localhost:8000/admin`
You will get the Above Result and it will ask you for admin credentials to login into it. if you have Admin credentials just log in and if don't then you need to create one.
You can create one by executing this command on your terminal
/> python manage.py createsuperuser
after executing the above command it will prompt you for some information like `Email Address`, `username`, `password` and `confirm password`. it will create an admin user for you and now you can log in using these credentials
As you can see by default it includes only two models or we can say tables that is `users` and `groups`. How to include our own models or tables into this interface? let's discuss this question in the next section
How To register model to the admin interface?
There are different approach to register models to the Django interface
Let say we have two models named `Author` and `Book`
Simple admin.site.register method
from django.contrib import admin from .models import Author, Book admin.site.register(Author) admin.site.register(Book)
using register decorator with ModelAdmin object
from django.contrib import admin from .models import Author, Book @admin.register(Author) class AuthorAdmin(admin.ModelAdmin): pass @admin.register(Book) class BookAdmin(admin.ModelAdmin): pass
How to unregister model from the admin interface?
Removing or unregistering the model from the admin interface is very simple you have to just write a single line of code
from django.contrib import admin from .models import Author, Book admin.site.unregister(Author) admin.site.unregister(Book)
Customizing Django Admin interface
Earlier I have mentioned that the Admin interface is highly customizable. so let's customize our admin interface
Changing `Django administration` text, `index title` and `site title`
admin.site.site_header = "BookStore Admin" admin.site.site_title = "BookStore Admin Portal" admin.site.index_title = "Welcome to BookStore Admin Portal"
Changing Models default list display, search fields, ordering and list_filters
from django.contrib import admin from .models import Author, Book @admin.register(Author) class AuthorAdmin(admin.ModelAdmin): list_display = ('name','email','is_verified') list_filter = ('name','email') search_fields = ('name', 'joined_date') ordering = ('-joined_date',) @admin.register(Book) class BookAdmin(admin.ModelAdmin): list_display = ('title','ISBN_NO','author_name') list_filter = ('title','ISBN_NO','author_name') search_fields = ('title','ISBN_NO','author_name') ordering = ('-created_at',)
These are the basics of customization and There are tons of other stuff that we can customize.
Django has a very vast community and there is some awesome developer who has created third party packages to customize Django
One of my Favorite ones is Django-Jazzmin which is a drop-in app with plenty of things you can easily customize, including a built-in UI customizer.
Thank You for Reading ‚. If you have any Suggestions or Query then let me know in the comment section
Article Contributed By :