pythondjangodjango-templatesdjango-admindjango-context

Django admin add context to index.html


I'm trying to override Django admin index.html to show table contain latest 5 records from a model

I managed to override the template with the following in my app:

from django.contrib import admin

admin.site.index_template = 'admin/dashboard.html'
admin.autodiscover()

But i have no idea how to add the latest records from model as a context to the index.html

Hope someone can help me


Solution

  • class CustomAdminSite(admin.AdminSite):
    
        def index(self, request, extra_context=None):
            extra_context = extra_context or {}
            # Add your context here
            return super(CustomAdminSite, self).index(request, extra_context)
    
    admin_site = CustomAdminSite()
    

    Don't forget to replace default admin with admin_site