4

When from a page , I goto another page via some hyperlink,is there any way to go back to the previous page. The previous page has some arguments also. SO I want to ask whether the previous page is saved somewhere or is there any other way to go back to that page

sahil
  • 1,108
  • 3
  • 15
  • 25
  • 1
    ? Aside from magic on the server. There is nothing to save the previous state of the page you've navigated away from. You must perform this task yourself by saving the information you require in the session. – Glycerine Dec 21 '10 at 15:06

2 Answers2

7

In http there is a header field called "referrer". If present it point to the previous page. You can access it from web2py:

if request.env.http_referer:
    redirect(request.env.http_referer)
Raydel Miranda
  • 13,825
  • 3
  • 38
  • 60
mdipierro
  • 4,239
  • 1
  • 19
  • 14
3

You have to be careful doing this if using a form on the page, since the initial load of the page will have the correct referer, however after submitting the form, the referer will be the page itself. To work around, I did something like this:

if session.back:
    redirect_url = session.back
else:
    redirect_url = URL()

# create form, do stuff, etc.
if form.accepts(request.vars.session):
    session.back = None
else:
    session.back = request.env.http_referer
Jason Martens
  • 1,305
  • 11
  • 22