O Hai Django AdminPlus

Last night, as happens sometimes, I was wishing it was possible to add some of our custom admin views to the Django admin’s index page. It’s kind of a pain to have to type the URL every time, especially when talking to other people: “It’s in the admin, but no, you can’t… just go to this URL.”

So I was reading the Django admin docs and, frankly, they aren’t great. They tell you about the benefits of subclassing AdminSite, or running multiple AdminSites, but they never really mention how to do it.

So, with a little trial and error, I wrote my AdminSite subclass and figured out how to use it. It simplified a bunch of code in our custom admin app, so I thought I would share it.

Here’s Django AdminPlus (and on PyPI).

AdminPlus adds a method, admin.site.register_view, that connects an arbitrary view function to your admin, and puts a link to it on the admin index page. If you put calls to register_view in someapp/admin.py, they’re picked up during the normal admin.autodiscover() process.

1.
# myapp/admin.py
  1. from django.contribimport admin
  2. from django.shortcutsimport rendertoresponse
  3.  
  4. def myadminview(request):
  5.     return rendertoresponse(‘myapp/admin/view.html’)
  6. admin.site.registerview(‘mypath’, myadmin_view)
  7.  
  8. # With an optional ‘name’ parameter:
  9. def myotheradmin_view(request):
  10.     return rendertoresponse(‘myapp/admin/other.html’)
  11. admin.site.registerview(‘otherpath’, myotheradminview, ‘Fancy Stuff!’)

Assuming your admin URLs start with admin/, this will make my_admin_view available at admin/mypath and my_other_admin_view available at admin/otherpath, but it also puts them in a new section on the admin index, so you don’t even need to worry about the URL.

If no name is given we try to guess from the name of the view function.

That’s about it! Read the README and the other docs and enjoy!