Add descriptive subheader for listing pages in django admin

This commit is contained in:
rachidatecs 2023-06-27 17:08:23 -04:00
parent d506e5a8e8
commit eac616204b
No known key found for this signature in database
GPG key ID: 3CEBBFA7325E5525
3 changed files with 48 additions and 2 deletions

View file

@ -141,6 +141,26 @@ class DomainApplicationAdmin(AuditedAdmin):
# Regular users can only view the specified fields # Regular users can only view the specified fields
return self.readonly_fields return self.readonly_fields
def changelist_view(self, request, extra_context=None):
if extra_context is None:
extra_context = {}
# Get the filtered values
filters = self.get_filters(request)
# Pass the filtered values to the template context
extra_context['filters'] = filters
extra_context['search_query'] = request.GET.get('q', '') # Assuming the search query parameter is 'q'
return super().changelist_view(request, extra_context=extra_context)
def get_filters(self, request):
filters = []
# Retrieve the filter parameters
for param in request.GET.keys():
# Exclude the default search parameter 'q'
if param != 'q' and param != 'o':
# Append the filter parameter and its value to the list
filters.append({'parameter_name': param.replace('__exact','').replace('_type','').replace('__id',' id'), 'parameter_value': request.GET.get(param)})
return filters
admin.site.register(models.User, MyUserAdmin) admin.site.register(models.User, MyUserAdmin)
admin.site.register(models.UserDomainRole, AuditedAdmin) admin.site.register(models.UserDomainRole, AuditedAdmin)

View file

@ -78,6 +78,12 @@ DEBUG = env_debug
# Installing them here makes them available for execution. # Installing them here makes them available for execution.
# Do not access INSTALLED_APPS directly. Use `django.apps.apps` instead. # Do not access INSTALLED_APPS directly. Use `django.apps.apps` instead.
INSTALLED_APPS = [ INSTALLED_APPS = [
# let's be sure to install our own application!
# it needs to be listed before django.contrib.admin
# otherwise Django would find the default template
# provided by django.contrib.admin first and use
# that instead of our custom templates.
"registrar",
# Django automatic admin interface reads metadata # Django automatic admin interface reads metadata
# from database models to provide a quick, model-centric # from database models to provide a quick, model-centric
# interface where trusted users can manage content # interface where trusted users can manage content
@ -106,8 +112,6 @@ INSTALLED_APPS = [
"django_fsm", "django_fsm",
# library for phone numbers # library for phone numbers
"phonenumber_field", "phonenumber_field",
# let's be sure to install our own application!
"registrar",
# Our internal API application # Our internal API application
"api", "api",
# Only for generating documentation, uncomment to run manage.py generate_puml # Only for generating documentation, uncomment to run manage.py generate_puml

View file

@ -0,0 +1,22 @@
{% extends "admin/change_list.html" %}
{% block content_title %}
<h1>{{ title }}</h1>
<h2>
{{ cl.result_count }}
{% if cl.get_ordering_field_columns %}
sorted
{% endif %}
results
{% if filters %}
filtered by
{% for filter_param in filters %}
{{ filter_param.parameter_name }} = {{ filter_param.parameter_value }}
{% if not forloop.last %}, {% endif %}
{% endfor %}
{% endif %}
{% if search_query %}
for {{ search_query }}
{% endif %}
</h2>
{% endblock %}