Linh Nguyen
Linh Nguyen

Reputation: 3890

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

Upvotes: 2

Views: 1551

Answers (2)

Davi Santos
Davi Santos

Reputation: 21

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

Django documentation

Upvotes: 1

Rin Nguyen
Rin Nguyen

Reputation: 455

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

Upvotes: 1

Related Questions