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.

# myapp/admin.py
from django.contribimport admin
from django.shortcutsimport render_to_response
def my_admin_view(request):
    return render_to_response(‘myapp/admin/view.html’)
admin.site.register_view(‘mypath’, my_admin_view)
# With an optional ‘name’ parameter:
def my_other_admin_view(request):
    return render_to_response(‘myapp/admin/other.html’)
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!