Django how to redirect pagination page after update data?

In a ListView I render a table using the Paginator with paginate_by = 5 . In each row I have a Button that opens the UpdateView . After a successful update I’m back on my ListView but always on the first page. How can I change the success_url so that I’m back on the page number, from where I opened the UpdateView ? Is there a proper way in Django to solve this?

class ApproveCommentPage(PermissionRequiredMixin,UpdateView):

      raise_exception = True

      permission_required = ("blog.add_commentblog","blog.change_commentblog","blog.delete_commentblog","blog.view_commentblog")

      model = BlogComment

      template_name = "approve_comment.html"

      form_class =AdminComment
             

      def get_success_url(self):

                    return reverse_lazy('blog-admin')

I find the solution after struggling lot of hours. Here I am sharing the code. Hope it will help others.

I added this success url in my update view:

def get_success_url(self):

        res = reverse('blog-admin')

        if 'page' in self.request.GET:

            res += f"?page={self.request.GET['page']}"

        return res

I added this mini forms in my list view. Basically this forms taking me to the update page after click on button.

<form method="POST" action="{% url 'comment-approve' pk=blogcomment.pk %}?page={{ page_obj.number }}">{% csrf_token %}<input type="submit" value="{{ blogcomment.name }}"></form>

After finishing edit it redirecting me on pagination page where I opened the update view. Such as if I opened it from page 5 then it will redirect me to page 5 from update page.

1 Like