djangodjango-admindjango-notification

Adding A Section To Admin Page


Ive changed certain parts of my admin page and played around extending the templates (so I have the file structure set up and working). I now want to go a bit further.

I want to add a column next to the 'recent activity' column on the admin page which would list all the most recent django-notifications the admin has received.

I am guessing I'd extend the base.html and add my notifications there. My first question is is this the best place to do this?

The next, bigger thing is that I would need to modify the original views.py file to do this. Surely this isnt a good idea? Poking around in the Django Admin views.py sounds like a terrible idea and makes me think that this kind of thing shouldnt be done, however id still like to know for sure before I give up on it.

I can find information on adding views to Django Admin, but nothing on adding content like this to the admin front page.

Thank you.


Solution

  • This won't be overly difficult to do.

    A rough outline, you'll need to use your own AdminSite, and overwrite the index method (this is the method which renders the main view). Something like this:

    from django.contrib import admin
    
    class MyAdminSite(admin.AdminSite):
    
        index_template = "path/to/my_template.html"
    
        def index(self, request, extra_context=None):
            # do whatever extra logic you need to do here
            extra_context = ...   # this will be added to the template context
            return super().index(request, extra_context=extra_context)
    

    Notice I've also added in a new index_template. This is what the index method will use to render it's response. So we had better set that up. We'll extend the old index.html and just add in our extra code in the bit that we want.

    {% extends "admin/index.html" %}
    {% block sidebar %}
    {{ block.super }}    // this just includes everything that was there before
    // add your extra html here
    {% endblock %}
    

    And this should do the trick :) So, no. You don't really have to go messing around with anything you shouldn't be.

    You'll want to register the MyAdminSite class as your new admin. There are clear instructions to do that in the django docs.

    However... It is always worth thinking about the first few lines from the django-admin docs:

    The admin’s recommended use is limited to an organization’s internal management tool. It’s not intended for building your entire front end around.

    The admin has many hooks for customization, but beware of trying to use those hooks exclusively. If you need to provide a more process-centric interface that abstracts away the implementation details of database tables and fields, then it’s probably time to write your own views.

    I would go a step further and say it should probably only be used by fellow developers. It's far to easy for others to cause major problems. So depending upon the reason why you want to add in these notifications here, it may or may not be a good thing to do.

    I hope this helps :)