Python Django admin将上下文添加到index.html

Python Django admin将上下文添加到index.html,python,django,django-templates,django-admin,django-context,Python,Django,Django Templates,Django Admin,Django Context,我试图重写Django admin index.html,以显示表中包含模型中最新的5条记录 我设法在我的应用程序中使用以下内容覆盖模板: from django.contrib import admin admin.site.index_template = 'admin/dashboard.html' admin.autodiscover() 但我不知道如何将模型中的最新记录作为上下文添加到index.html 希望有人能帮助我 class CustomAdminSite(admin.A

我试图重写Django admin index.html,以显示表中包含模型中最新的5条记录

我设法在我的应用程序中使用以下内容覆盖模板:

from django.contrib import admin

admin.site.index_template = 'admin/dashboard.html'
admin.autodiscover()
但我不知道如何将模型中的最新记录作为上下文添加到index.html

希望有人能帮助我

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()

别忘了用
admin\u站点

替换默认的
admin
,我添加额外上下文的方法是使用上下文处理器。解释如下: