2

I am trying to use HTTP_REFERER to create a link to the previous page. However, let's say I have a 'page1' that calls the 'page2' get method. It will show a form at the 'page2' where I can do a post request. When I post on the 'page2', the HTTP_REFERER is referencing the same page (page2), therefore I can't to back to the 'page1'. Is there anything I can do to go back to the 'page1'?

almanegra
  • 653
  • 8
  • 21
  • Going back to previous page after post request: [link](http://stackoverflow.com/questions/35796195/how-to-redirect-to-previous-page-in-django-after-post-request) – dentemm Apr 11 '17 at 17:11
  • It won't work, because the request is coming from the same page. – almanegra Apr 11 '17 at 17:19
  • This will work. The method is setting an additional hidden field on your form which captures the referring url. The post request will then be on the same page, and you redirect to that hidden field which was the url before that. – dentemm Apr 11 '17 at 17:34
  • The next attribute keeps pointing to the current URL, even though I used . My guess is that {{ request.path }} is loading the request path of the GET method that I did before the post. – almanegra Apr 11 '17 at 17:40
  • The value for the next url should be your HTTP_REFERER – dentemm Apr 11 '17 at 17:41
  • Oh....sure! I didn't realize it. Create an answer that I will take as accepted. Thanks – almanegra Apr 11 '17 at 17:43
  • I read this, for a back button/link https://stackoverflow.com/questions/524992/how-to-implement-a-back-link-on-django-templates – AnonymousUser Apr 08 '22 at 04:55

1 Answers1

1

You will need to add a hidden next field to your form . This will capture the previous page by making use of the HTTP_REFERER. After your post request you can then perform a redirect to the page you were on before the form page.

Edit: More information can also be found here.

Community
  • 1
  • 1
dentemm
  • 6,231
  • 3
  • 31
  • 43