1

I need to display a Django message on the Django admin index site. I am looking for a way to add certain conditions for the message to be displayed and pass it to Django index site. Is there a way to achieve that?

Suz
  • 11
  • 1

1 Answers1

0

You should do this by customizing your django admin stie. In the admin.py module of your app you should define a new AdminSite and override the index method. In the index method you can add your custom messages:

# [<app>/admin.py]

from django.views.decorators.cache import never_cache
from django.contrib import admin

class CustomAdminSite(admin.AdminSite):
    @never_cache
    def index(self, request, extra_context=None):
        messages.add_message(request, messages.INFO, 'This is a message.')
        return super().index(request, extra_context)

Then don't forget to change the default admins site to your CustomAdminSite:

# [<app>/admin.py]

admin_site = CustomAdminSite(name='myadmin')

Then in the project's urls.py use your custom admin_site:

# [<project>/urls.py]

from <app>.admin import admin_site

urlpatterns = [
    ...
    path('', admin_site.urls),
]