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
2.
from django.contribimport admin
3.
from django.shortcutsimport render_to_response
4.
 
5.
def my_admin_view(request):
6.
    return render_to_response(‘myapp/admin/view.html’)
7.
admin.site.register_view(‘mypath’, my_admin_view)
8.
 
9.
# With an optional ‘name’ parameter:
10.
def my_other_admin_view(request):
11.
    return render_to_response(‘myapp/admin/other.html’)
12.
admin.site.register_view(‘otherpath’, my_other_admin_view, ‘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!