How to extend django admin view?

kierzniak picture kierzniak · Oct 21, 2011 · Viewed 9k times · Source

i want to display additional sidebar in my django admin index. I have created templates/admin/index.html and it show up. Now i need some data from model. To do this I have created index function in the file admin/views.py

def index(request):
    var = 'var'
    return render_to_response('admin/index.html', {'var': var})

Without this function I have error ViewDoesNotExist.

However template not react for this sample variable 'var'. Moreover my app doesn't display in the index. I have only auth app.

I think that I'm overwriting index function form admin view. How to properly overwrite this function?

Answer

Timmy O'Mahony picture Timmy O'Mahony · Oct 21, 2011

Instead of overwriting the view entirely, you can add logic to the views in your ModelAdmin (admin.py) class:

https://docs.djangoproject.com/en/dev/ref/contrib/admin/#other-methods

so for example:

class MyAdmin(admin.ModelAdmin)
    ...
    def add_view(self, request, form_url='', extra_context=None):
        # Do some extra queries that will get passed to the template
        c = {'x':SomeModel.objects.all()}
        super(MyAdmin, self).add_view(request, extra_context=c)