django admin, extending admin with custom views

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.


The guide you linked is old and I was surprised to not find anything directly answering your question in the last year or so.

  1. Create a new Admin Site in your app's admin.py or in a convenient place.
  2. Create a function in the new AdminSite that augments the get_urls() function with your extra urls.
  3. Make sure your project urls.py links to the newly created AdminSite.

The below works with Python 3.5.1 and Django 1.9.6.


my_app/admin.py

from django.contrib import admin
from django.contrib.admin import AdminSite
from django.http import HttpResponse

from my_app.models import SomeModel


class MyAdminSite(AdminSite):

    def custom_view(self, request):
        return HttpResponse("Test")

    def get_urls(self):
        from django.conf.urls import url
        urls = super(MyAdminSite, self).get_urls()
        urls += [
            url(r'^custom_view/$', self.admin_view(self.custom_view))
        ]
        return urls

admin_site = MyAdminSite()


@admin.register(SomeModel, site=admin_site)
class SomeModelAdmin(admin.ModelAdmin):
    pass

my_project/urls.py

from django.conf.urls import url, include

from my_app.admin import admin_site

urlpatterns = [
    url(r'^admin/', admin_site.urls),
    ...
]

Tags:

Python

Django