pythondjangodjango-admin

NoReverseMatch at /admin/ Reverse for 'app_list' with keyword arguments '{'app_label': 'admin_index'}' not found


i setup a custom admin page , where added link to page i want , the code worked fine until itried to utilize dajngo_admin_index leading to app_list not being detected , The project has only 1 app named myapp

this my admin.py :

# Customizing the Admin Interface
class CustomAdminSite(admin.AdminSite):
    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path('analytics/', self.admin_view(analytics_view), name='custom_analytics'),
        ]
        return custom_urls + urls 

custom_admin_site = CustomAdminSite(name='custom_admin')

this my urls.py for admin :

from django.urls import path, include
from django.conf.urls.static import static
from django.conf import settings
from myapp.admin import custom_admin_site  

urlpatterns = [
    path('admin/', custom_admin_site.urls),  # Register custom admin URLs
    path('', include('myapp.urls')),
]

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

this new admin template base_site.html :

{% extends "admin/base.html" %}

{% block branding %}
<h1 id="site-name"><a href="{% url 'admin:index' %}">SDM Administration</a></h1>
{% endblock %}

{% block user-tools %}
<ul class="user-tools">
    <li><a href="#">Test Link</a></li>
    <li><a href="{% url 'admin:password_change' %}">Change password</a></li>
    <li><a href="{% url 'logout' %}">Log out</a></li>
</ul>
{% endblock %}

{% block nav-global %}
<ul class="nav-global">
    <li>
        <a href="{% url 'admin:custom_analytics' %}" class="reports-link">Reports and Analytics</a>
    </li>
</ul>
<style>
    /* Styling for nav-global and reports-link */
    .nav-global {
        display: inline-block;
        margin-left: 30px;
        margin-top: 10px;  /* Move the button down by 3px */
        list-style-type: none;  /* Remove the bullet (yellow box) */
        padding-left: 0;  /* Ensure no padding is left */
    }
    .nav-global > li {
        display: inline-block;
    }
    .nav-global .reports-link {
        color: #fff;
        background-color: #2B3C4D; /* Blue background color */
        padding: 5px 15px;
        border-radius: 3px;
        text-decoration: none; 
        font-weight: bold;
        transition: background-color 0.3s ease;
    }
    .nav-global .reports-link:hover {
        background-color: #1d5a8a;
    }
</style>
{% endblock %}

any

itried to change custom admin to inherit the app list but it didnt work:

class CustomAdminSite(AdminSite):
    site_header = "SDM Administration"
    site_title = "SDM Admin Portal"
    index_title = "Welcome to the SDM Admin Portal"
    site_url = '/tree/'

    def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path('analytics/', self.admin_view(analytics_view), name='custom_analytics'),
        ]
        return custom_urls + urls

    def app_list(self, request, app_label):
        context = {
            **self.each_context(request),
            'title': self.get_app_list_title(app_label, request),
            'app_label': app_label,
            'app_url': '',
        }
        return TemplateView.as_view(template_name='admin/app_list.html', extra_context=context)(request)

custom_admin_site = CustomAdminSite(name='custom_admin')

i do believe there is issue on how i setup my custom admin interface , leading to the custom admin is not properly inheriting all components, like app_list, which django-admin-index expects.


Solution

  • The issue here (partly) is that the app_list URL pattern is added conditionally to the admin site, specifically it is added when there is at least one model registered to the admin site (So that the app label is considered "valid" by the admin site).

    What's going wrong in your case is two fold:

    Normally this package you're using won't work well with custom admin sites but given your url patterns it doesn't look like you want multiple admin sites, so you can simply override the default admin site to fix this issue. In an app of yours create a file (let's say apps.py) with contents like so:

    from django.contrib.admin.apps import AdminConfig
    
    
    class MyAdminConfig(AdminConfig):
        default_site = "path.to.CustomAdminSite" # Update "path.to" to the actual module path
    

    Then in your INSTALLED_APPS setting django.contrib.admin with the path to this class:

    INSTALLED_APPS = [
        # ...
        "path.to.MyAdminConfig",  # replaces 'django.contrib.admin'
        # ...
    ]