2

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

Linh Nguyen
  • 3,452
  • 4
  • 23
  • 67

2 Answers2

1
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

Rin Nguyen
  • 405
  • 4
  • 11
1

Something that worked for me to add extra context was using context processor. Explained here: https://stackoverflow.com/a/34903331/13436391

Django documentation