Add view in admin panel with same design as others

Viewed 44

I'm trying to create a documentation part in my admin panel, and for that I created a custom admin site. I've overriden the get_app_list and get_urls methods in order to display a new line in the navigation bar and the dashboard, and I created a view to which this line redirects :

class MyAdminSite(admin.AdminSite):    
    def get_app_list(self, request):
        app_list = super().get_app_list(request)
        app_list += [
            {
                "name": "Documentation",
                "app_label": "my_test_app",
                "models": [
                    {
                        "name": "Scripts modification",
                        "object_name": "Scripts modification",
                        "admin_url": "/admin/documentation/modif/",
                        "view_only": True,
                    }
                ],
            }
        ]
        return app_list
    
    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path('documentation/modif/', self.admin_view(self.modif)),
        ]
        return my_urls + urls
    
    def modif(self, request):
        context = {
            'title':"Documentation",
        }
        return render(request, "documentation/scripts_modification.html", context)

Everything works correctly and when clicking on the new line, I'm redirected to the new view I created. However, I want my view to have the same design as the rest of the admin panel, which I didn't how to do.

I extended admin/base_site.html but I only get the top of the page, and not the view site log out panel nor the navigation sidebar :

enter image description here

I have no idea what I should change/configure in order to have the same display as the other admin pages. Shouldn't extending base_site.html display what I want ? There's something I didn't really understand here and I can't find what nor where to learn about it.

1 Answers

I finally found the reason why it wasn't working!

There are a lot of if clauses in admin/base.html (which is extended in admin.base_site.html) that check the values of several variables.

In order to display the top right panel, the variable has_permissions must be set to True, and to have the view site link, the site_url should be set to the url of the site.

Now, to display the navigation bar, is_nav_sidebar_enabled need to be set to True and available_apps must contains all the apps to display.

By adding all of that to the context of the template, everything is rendered correctly like on the rest of the admin panel.

And my code now looks like that :

class MyAdminSite(admin.AdminSite):    
    def get_app_list(self, request):
        app_list = super().get_app_list(request)
        app_list += [
            {
                "name": "Documentation",
                "app_label": "documentation",
                "models": [
                    {
                        "name": "Scripts modification",
                        "object_name": "Scripts modification",
                        "admin_url": "/admin/documentation/modif/",
                        "view_only": True,
                    }
                ],
            }
        ]
        return app_list
    
    
    def get_urls(self):
        urls = super().get_urls()
        my_urls = [
            path('documentation/modif/', self.admin_view(self.modif)),
        ]
        return my_urls + urls
    

    def modif(self, request):
        context = {
            'title':"Documentation",
            'has_permission': True,
            'site_url': reverse('scripts:scripts'),
            'is_nav_sidebar_enabled': True,
            'available_apps': self.get_app_list(request),
        }
        return render(request, "documentation/scripts_modification.html", context)
Related