Saturday, 7 May 2022

How to enable django admin sidebar navigation in a custom view?

I have a view inheriting from LoginRequiredMixin, TemplateView, which renders some data using the admin/base_site.html template as the base. I treat it as a part of the admin interface, so it requires an administrator login. I'd like to make this view a little bit more a part of the Django admin interface by enabling the standard sidebar navigation on the left-hand side.

Note that I don't have a custom ModelAdmin definition anywhere, I simply render the template at some predefined URL. There are no models used in the interface either, it parses and displays data from database-unrelated sources.

Currently, I just build the required context data manually, e.g.:

data = super().get_context_data(**kwargs)
data.update(**{
    "is_popup": False,
    "site_header": None,
    "is_nav_sidebar_enabled": True,
    "has_permission": True,
    "title": "My title",
    "subtitle": None,
    "site_url": None,
    "available_apps": []
})

The sidebar is visible, but displays an error message:

Error message: You don't have permission to view or edit anything

Adding an app to available_apps ("available_apps": ["my_app"]) doesn't help either:

Rendered sidebar with no data

So my question is - how do I do that? Is there a class I can inherit from to achieve this behaviour? Or a method I can call to get all required context data for base_site.html? Or perhaps I should insert some information in my template? Perhaps I need an AdminSite object, or can somehow call methods of the default one?



from How to enable django admin sidebar navigation in a custom view?

No comments:

Post a Comment