Django管理员将上下文添加到index.html [英] Django admin add context to index.html

查看:64
本文介绍了Django管理员将上下文添加到index.html的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试覆盖Django admin index.html,以显示表包含模型中的最新5条记录

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

但是我不知道如何将模型中的最新记录作为上下文添加到index.html

But i have no idea how to add the latest records from model as a context to the 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_site

这篇关于Django管理员将上下文添加到index.html的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆