UpdateView does not return to original page in paginated ListView

I am new to Django and this is my first question, so apologies up front if the answer is obvious.

I have a large number of transactions that I am displaying using a cbv ListView, paginated by 10. Each transaction is editable using an UpdateView. If I am on say page 50, edit a record and then I submit it, I return to my ListView, but always on the first page, not page 50. What is the correct approach to change the success_url so that I return to the page in the list from where I opened the UpdateView. After much googling, it would appear that this is a common problem, but there is no particular way to handle this. Is there simple solution offered by the generic class based views?

The admin views do have this behaviour, where you return to the correct page in a paginated list after opening the record. I’ve looked at the code behind this however it seems quite complex and involves preserving the filters. It also does not appear to use class based views.

One option is to redirect to the value of the referer header, if it’s set and looks like a URL for your list view (on the current domain). This has problems if the form shows errors though, since the referer will be the same page; in that case, you can use the first load of the form to store the referer in the form in a hidden field, and read that on success to redirect.

I solved this providing a GET Parameter of page in my template:

{% url 'namespace:update' object.pk %}?page={{ page_obj.number }}

And in the Updateview:

class ModelUpdate(UpdateView):
    get_success_url(self):
        page_number = self.request.GET['page']
        return f'{reverse(namespace:list)}?page={page_number}'
1 Like

Many thanks, will try this out.

Thank you so much, sir! :clap:
Spent 4h for finding solution…