To do that, you’ll have to create your own AdminSite - different from the default admin site.
In your app’s admin.py file or elsewhere, create a subclass of the admin.AdminSite class.
# assuming yourapp/site.py
from django.contrib import admin
class YourCustomAdminSite(admin.AdminSite):
# you can override the default admin interface that was created by django
# with this, but it also means you'll have to change your project/urls.py and
# register your models with this admin site instance
site_header = "Custom Admin"
site_title = "Custom Admin"
index_title = "Custom Admin"
# now create an instance of your new admin site
custom_admin_site = YourCustomAdminSite()
# assuming yourapp/admin.py
from .models import YourModel
from .site import custom_admin_site
custom_admin_site.register(YourModel)
# in your project root's urls.py
from django.urls import path
from yourapp.site import custom_admin_site
urlpatterns = [
path('admin/', custom_admin_site.urls), # replaces admin.site.urls
]
Note that even though you’ve now created a custom AdminSite, it’ll still use the default Django mechanism for creating admin pages and its default templates.
You’ll need to create a custom templates/admin folder with templates that override
-– base.html
-– base_site.html
-– index.html
-– …etc
In you case, you want the matplotlib from http://localhost:8000/admin/ - to achieve this, you’ll need to override the index_view in your custom_admin_site class so that you can pass in the context of the matplotlib instance
# ...previous code
from django.template.response import TemplateResponse
class YourCustomAdmin(admin.AdminSite):
# previous code
def index_view(self, request, extra_context=None, **kwargs):
# now you can create your matplotlib and render
context:dict = self.each_context(request) # get the default context
template = "admin/index.html" # or your custom template
context.update({
'matplotlib': self.your_matplotlib_func(),
# **extra_context if extra_context
})
return TemplateResponse(request, template, context)