Adding "Previous" and "Next" buttons in Django Admin's Change Form

#Python #Django

In the same application where I used some DB denormalization, I was also asked to add buttons to go directly from a contact’s edit page to the edit page for the previous or next contact.

I first thought there would be some magical invocation in the ModelAdmin definition - like

class ContactAdmin(admin.ModelAdmin):
    [...]
    # does not work!!
    previous_next_buttons = True

But this option does not exist. Thinking about it, it does make sense: previous and next are quite contextual notions, so you’ll have to implement them yourself.

Fortunately, it happenend to be very easy to do:

(These definitions are arguably too simplistic… but as I said, this is the context-dependant part, so you’ll have to find by yourself what’s significant in your case!)

And voilà! Your change form now has the desired buttons. (As you can see below, I added some other buttons too, but the previous and next are there)

Screenshot of the result

Update June 14th, 2012 Fixed a few errors pointed out by David Feinzeig. Thanks!