开发者

web2py redirect to previous page

开发者 https://www.devze.com 2023-02-01 03:03 出处:网络
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 toask whether the previous page is save

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 th开发者_StackOverflowat page


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)


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
0

精彩评论

暂无评论...
验证码 换一张
取 消