django admin, extending admin with custom views

前端 未结 4 1517
花落未央
花落未央 2021-02-07 13:06

I would like to request some assistance regarding this matter.

I have followed this guide to add a view to my admin.

I am using the same code that the site has a

4条回答
  •  忘掉有多难
    2021-02-07 13:38

    This guide looks quite old. I would rather advise you to follow django docs.

    someapp/admin.py

    from django.contrib.admin import AdminSite
    from django.http import HttpResponse
    
    class MyAdminSite(AdminSite):
    
         def get_urls(self):
             from django.urls import path
             urls = super().get_urls()
             urls += [
                 path('my_view/', self.admin_view(self.my_view))
             ]
             return urls
    
         def my_view(self, request):
             return HttpResponse("Hello!")
    
    admin_site = MyAdminSite()
    

    Source: https://github.com/django/django/blob/2.2/django/contrib/admin/sites.py#L194-L205

    You should also update your project/urls.py and replace path('admin/', admin.site.urls) by path('admin/', admin_site.urls). Don't forget to from someapp.admin import admin_site before.

提交回复
热议问题