How to Add Multiple Admin Sites to Your Django App
One of the nice aspects of Django Web Framework is the inbuilt admin site that gives you access to your models with all the CRUD functionalities.
In this post, we will explore how to add an additional admin site to the default admin site.
This is useful in case you are building a large scale web application with different levels
of admin and you dont want to use groups but rather isolate each group of admin to their own dashboard -eg admin for editors admin for site owners or admin for devs
To accomplish this django provides a simple way to do so.
In our case the main admin page will be our default for developers and then we will create
a new admin site for editors who can add posts and make edits
# blogapp/admin.py
from django.contrib import admin
from .models import PostsModel, CommentsModel
# Register your models here.
admin.site.register(PostsModel)
admin.site.register(CommentsModel)
For our additional admin page we will create another file named editors_admin.py and them import AdminSite, the generic base class and create a new adminsite class that inherits all the properties of the default admin as below.
# blogapp/editors_admin
from django.contrib.admin import AdminSite
from .models import CommentsModel
# Create a New Admin Dashboard
class EditorsAdminSite(AdminSite):
site_header = "Editors Admin"
site_title = "Editors Admin Portal"
index_title = "Editors Admin List"
editors_admin_site = EditorsAdminSite(name='editors_admin')
# register a model
editors_admin_site.register(CommentsModel)
We will then register the models we want to show on the Editors Admin page like we would have done for the general admin page. That is it. The next task it to include this new editors admin site urls to the project urls like below
# blogproject/urls.py
from django.contrib import admin
from django.urls import path,include
from blogapp.editors_admin import editors_admin_site
urlpatterns = [
path('admin/', admin.site.urls),
path('editor_admin/', editors_admin_site.site.urls),
]
You can now see this additional admin page by navigating to the url
http://localhost:8000/editor_admin
That is it, we have seen how to add an additional admin page to our django application.