#Model admin views

8 messages · Page 1 of 1 (latest)

barren slate
#

Hello community,

I'm trying to implement a custom view/site for my model. I followed the documentation, and the view loads correctly — the link to the view also appears on the changelist page. So far, everything is working as expected.

However, I would like to know the proper way to include the sidebar and/or important fields in a custom admin view.
Thanks in advance!

brave blade
#

Are you asking about handling django's admin page?

barren slate
#

I'm asking what is the proper way to make a new custom view for my model including the django admin sidebar (or any other necesary context, for usability or security reasons), currently I'm extending from base_site.html.

golden prawn
#

Imho, the admin is made not obvious to extend because i personally think of it as a fancy db Explorer, and anything else should be written as your own application views

barren slate
#

I figured it out, I used something called, each_context, or something like that, and after that the sidebar appeared.
edit: I recently notice that is on the get_urls docs, thanks @golden prawn for your time.

barren slate
#
def get_urls(self):
        urls = super().get_urls()
        custom_urls = [
            path("custom/", self.admin_site.admin_view(self.custom_view), name="custom"),
        ]
        return custom_urls + urls

def custom_view(self, request):
        base_context = self.admin_site.each_context(request)
        context = {
            **base_context
        }
        return TemplateResponse(request, "admin/core/price/custom_view.html", context=context)

To clarify what worked for me

barren slate