How to get the next and previous object from a list of objects or queryset

So I came across something the other day I figured I would share.

With django's help, I made a photo app. Each photo can belong to multiple Projects. I can get a list of photos for a Project and display their thumbnails. When I click on a thumbnail it pops up the large version of the photo in a nice div on the screen. I wanted to be able to click "next" or "previous" and get the next or previous photo in the project, and not the next or previous photo from the entire list of photos that is returned by my manager.

{{Photo.objects.get_next_by_created.url}}

I determined I would need to figure out where the current photo was in the list of photos for that project. So I needed the current photo's index. Then I just needed to take that index and +1 or -1 for the next or previous photos.

Here is how I did that: