0

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.

horse
  • 479
  • 7
  • 25

1 Answers1

2

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

Community
  • 1
  • 1
tim-mccurrach
  • 6,395
  • 4
  • 23
  • 41
  • Thank you very much. Your instructions are clear im confident I can get it working. I just have one question, Im wondering where I should place your first block of code,```class MyAdminSite```? Theres also a lot of learning here, even just that ```{{ block.super }}``` tag is new to me and has given me something to look in to. – horse Jun 04 '20 at 22:02
  • @Brenden You're very welcome :) That's actually a very good quesion. And not clear at all, so thanks for the follow up question. I've edited my answer to include a link to the django docs, which provide some very clear instructions for that particular step. – tim-mccurrach Jun 04 '20 at 22:24
  • 2
    Also, yes it took me a while to learn about `block.super` but it really is very useful. When you extend a parent template, and use `{% block some_block %}` you actually overwrite the whole block from the parent template. Most of the time this isn't what you want. Normally you want to just include a little bit extra. `{{ block.super }}` just says, include everything from this block in the parent template here, and then you can write your extra bit. – tim-mccurrach Jun 04 '20 at 22:26