Tutorial: Using Django SB AdminPlus
This tutorial will guide you through creating custom admin views using django-sb-adminplus. We will build upon a simple demo application and demonstrate how to register different types of custom views in the Django Admin.
Prerequisites¶
Make sure you have installed the package and added it to your Django project.
Your urls.py should be configured to use AdminSitePlus instead of the default AdminSite:
# urls.py
from django.contrib import admin
from django.urls import path, re_path
from django_sb_adminplus.sites import AdminSitePlus
admin.site = AdminSitePlus()
admin.autodiscover()
urlpatterns = [
re_path(r"^admin/", admin.site.urls),
]Creating Custom Admin Views¶
You can add custom views to your admin.py files. django-sb-adminplus allows you to register both function-based views (FBVs) and class-based views (CBVs) directly into the admin interface.
1. Basic Function-Based View¶
Let’s start by creating a simple function-based view. To make it integrate well with the admin interface, we pass admin.site.each_context(request) to the template context.
# demoapp/admin.py
from django.contrib import admin
from django.shortcuts import render
def basic_custom_view(request):
"""A simple function-based view registered in the admin."""
context = dict(
admin.site.each_context(request),
title="Basic Custom View",
message="This is a simple function-based view running inside the Django Admin.",
)
return render(request, "demoapp/basic.html", context)
# Register the view
admin.site.register_view("basic", view=basic_custom_view, name="Basic Custom View")Now, your view will be available at /admin/basic/.
2. Advanced Class-Based View¶
You can also register Django class-based views (like TemplateView). Just remember to update the context data with the admin context.
# demoapp/admin.py
from django.contrib import admin
from django.views.generic import TemplateView
class AdvancedCustomView(TemplateView):
"""A class-based view registered in the admin."""
template_name = "demoapp/advanced.html"
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# Add the admin context so the template inherits the admin layout
context.update(admin.site.each_context(self.request))
context["title"] = "Advanced Custom View"
context["message"] = "This is a Class-Based View (CBV) running inside the Django Admin."
return context
# Register the view using the .as_view() method
admin.site.register_view("advanced", view=AdvancedCustomView.as_view(), name="Advanced CBV")This view will be available at /admin/advanced/.
3. Conditional Visibility¶
Sometimes you want a custom view to be accessible, but only visible in the admin sidebar to certain users, such as superusers. You can achieve this using the visible argument.
# demoapp/admin.py
from django.contrib import admin
from django.shortcuts import render
def secret_view(request):
"""A view only visible if the user is a superuser."""
context = dict(
admin.site.each_context(request),
title="Secret View",
message="You can see this because you are a superuser!",
)
return render(request, "demoapp/secret.html", context)
def is_superuser(request):
return request.user.is_superuser
# Register the view with the visible flag
admin.site.register_view(
"secret",
view=secret_view,
name="Secret Superuser View",
visible=is_superuser
)Now, the “Secret Superuser View” link will only appear in the admin index and sidebar if the user is a superuser.
Conclusion¶
With django-sb-adminplus, extending the Django Admin is straightforward. You can easily add dashboards, custom reports, and configuration pages without having to bind them to a specific model.