django admin: custom app_index with context

1.2k views Asked by At

In django admin, we can define custom templates per app. In this case, I'm customising the app_index.html template for my application.

I'd like to add a few graphs and other to that page. Now that I've overridden the template, how can I override the corresponding view method?

I thought about making a custom AdminSite and override the app_index() (see https://github.com/django/django/blob/master/django/contrib/admin/sites.py#L511) method, but I have more than one application in my django installation, all of which will have a custom app_index.html.

What's the best way to add context to the app_index.html template?

1

There are 1 answers

0
Torkel Bjørnson-Langen On

Don't know if this is the best way, but it can be done with template tags. Here is how I did it:

# <app>/templatetags/erp.py
register = template.Library()
@register.assignment_tag
def erp_get_tasks ():
    return Task.objects.exclude (done=True).order_by ('priority')

.

# <app>/templates/admin/erp/app_index.html
{% extends "admin/app_index.html" %}
{% load erp %}
...
{% block footer %}
{% erp_get_tasks as tasks %}
{% for task in tasks %}
...