# Best practices to redirect user to previous page after form submit?

**URL:** <https://forum.djangoproject.com/t/best-practices-to-redirect-user-to-previous-page-after-form-submit/4729>\
**Category:** Using Django\
**Created:** [October 10, 2020, 11:18am UTC](https://forum.djangoproject.com/t/best-practices-to-redirect-user-to-previous-page-after-form-submit/4729 "2020-10-10T11:18:19Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [October 10, 2020, 11:18am UTC](https://forum.djangoproject.com/t/best-practices-to-redirect-user-to-previous-page-after-form-submit/4729/1 "2020-10-10T11:18:19Z")

</div>

Hello,

in my webapp I have multiple places with “Edit” button that redirects user to form to edit particular model instance. I am currently redirecting to admin list for all the models. However what I really want is to redirect to previous page. For example:

Page A -\> Form -\> Submit -\> Page A  
Page B -\> Form -\> Submit -\> Page B  
etc

What is the correct way to do this? I think I could save the original page into session and then read it just after successful POST, but maybe there is more appropriate solution? If this could be abstracted into a decorator, that would be fantastic.

Thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 10, 2020, 3:58pm UTC](https://forum.djangoproject.com/t/best-practices-to-redirect-user-to-previous-page-after-form-submit/4729/2 "2020-10-10T15:58:12Z")

</div>

In part, how you handle this is going to depend upon your view - the answers will change depending upon whether you’re using a function-based view, a class-based view, or one of the Django-provided Generic CBVs.

What does your view look like?

---

<div class="post-metadata">

**Author:** ![nemecek\_f](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/nemecek_f/32/668_2.png) [@nemecek\_f](https://forum.djangoproject.com/u/nemecek_f)\
**Post date:** [October 10, 2020, 6:42pm UTC](https://forum.djangoproject.com/t/best-practices-to-redirect-user-to-previous-page-after-form-submit/4729/3 "2020-10-10T18:42:22Z")

</div>

I am using FBVs. Not sure why it did not occur to me to specify this in the first post. The code is fairly straightforward, I am creating form instance from the `request.POST` and then seeing whether the form is valid. If so then I save the edit and redirect to the list (also FBV), if not will display the form again with errors.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [October 10, 2020, 9:45pm UTC](https://forum.djangoproject.com/t/best-practices-to-redirect-user-to-previous-page-after-form-submit/4729/4 "2020-10-10T21:45:41Z")

</div>

There are a couple different ways that I have done this in the past.

In those cases where I have two urls pointing to the same view (one with a pk as a parameter for an edit function and one without, using that same function as a create function), I’ve done a redirect using either the [`get_absolute_url`](https://docs.djangoproject.com/en/3.1/ref/models/instances/#get-absolute-url) method on models or the [`reverse`](https://docs.djangoproject.com/en/3.1/ref/urlresolvers/#reverse) method passing the pk as an arg for the url.

In those cases where the view method isn’t used for a create - it’s an edit-only function for a model instance, I don’t redirect at all. Since I’m returning to the same url, I have my function return the same response for the POST as for the GET.

Ken
