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?
Asked
Active
Viewed 274 times
1
-
are you looking for description kind of message explaining how a field is to be created in admin panel ?? – SANGEETH SUBRAMONIAM Apr 01 '21 at 04:14
-
I trying to show an alert message on the admin:index page when a certain condition is true. – Suz Apr 02 '21 at 08:20
1 Answers
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),
]

Ali Satvaty
- 69
- 3