How to add Back and Forward buttons to the Django Admin paginator
Move to the Next or Previous page with custom buttons

Last Updated: None

This is such a basic feature and was such an easy fix I was surprised I didn’t find a snippet to add Back/Forward (or Next/Previous?)buttons in the Django Admin. (Or does it exist, and was I just using the wrong search terms?)

What am I talking about?

I currently work for a company that has hundreds of magazine articles in the database. The Head of Content had to add citations to several hundred documents, and she asked me if I could add some Back and Forward buttons to the admin interface, to make this tedious task a little easier for her.

So here’s how I did it. It only takes 2 files and not even that much code.

1. Overriding admin templates

You may already know that overriding the Django admin templates is not that hard. Pretty much all you have to do is put a directory called adminsettings.py (so django knows to look for it) and name it after the same file that django would be using otherwise (so django knows what to do with it).

For example, my project directory might look something like this example on Stack Overflow

In your templates directory, add a directory called admin and in that directory, create a file called pagination.html.This is what mine looks like:

What’s content_extras, you may be wondering. I’m so glad you asked!

2. Creating the custom template tags

You don’t need to call your custom template tags file content_extras, I just did that because the project already had the file. You can put it in whatever app you want (as long as it’s an app that’s registered in the settings file) and you don’t need to call it something_extras, that’s just convention.

What is important is:

  • You put it in an app that’s registered in the settings.py file, via the INSTALLED_APPS list variable (I already said that)
  • In the app’s directory, you include another directory called templatetags. Don’t forget the __init__.py file.

Whatever you call the file is what you will need to include in your template. Notice our file is called content_extras.py, so that’s why my template templates/admin/pagination.html, has {% load content_extras %} in it.

Now, in your content_extras.py, (or whatever you call it) include two new functions:

That’s it!

You basically end up mimicking the django template tag pagination_number, in order to keep the rest of the query string in case your user has applied any list filters. (I don’t know if DOT is necessary. Sure, why not?)

Just be advised that this will be applied to ALL of your models in the admin. For example, the Content Head is the one that asked me to do it, and I hope the Clinical Content team is okay with it too!

Connect With Us


info@teamkaffeeklatsch.com