How to redirect to referring page?

79 views
Skip to first unread message

Zeynel

unread,
Jan 17, 2011, 12:52:35 PM1/17/11
to Google App Engine
Hi,

I just added a new page "/hot" to sort by number of votes to articles.

VoteHandler handles the vote with

item.vote +=1
item.put()
self.redirect("/newest")

but now the new page "/hot" also refers to this VoteHandler.

After writing the new vote to the database I need to redirect to
either /hot or /newest depending on the referrer.

I searched the group and stackoverflow
http://stackoverflow.com/search?page=2&tab=relevance&q=redirect%20google%20app%20engine
but I could not find the answer.

Can anyone direct me to the right reference?

Thanks.

Robert Kluin

unread,
Jan 17, 2011, 1:26:59 PM1/17/11
to google-a...@googlegroups.com
Hi Zeynel,
Assuming webapp, how about:
if self.request.referrer == '/whatever':
self.redirect('/somespot')
elf self.request.referrer == '/other':
self.redirect('/aplace')
else:
self.redirect('/there')

Webapp's self.request is a WebOb request, so it is documented here:
http://pythonpaste.org/webob/reference.html#id1

Robert

> --
> You received this message because you are subscribed to the Google Groups "Google App Engine" group.
> To post to this group, send email to google-a...@googlegroups.com.
> To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.
>
>

Zeynel

unread,
Jan 17, 2011, 2:28:39 PM1/17/11
to Google App Engine
Thanks! But I noticed that in this case relative urls do not work. I
am not sure why. In the rest of the script all urls are relative.


On Jan 17, 1:26 pm, Robert Kluin <robert.kl...@gmail.com> wrote:
> Hi Zeynel,
>   Assuming webapp, how about:
>     if self.request.referrer == '/whatever':
>         self.redirect('/somespot')
>     elf self.request.referrer == '/other':
>         self.redirect('/aplace')
>     else:
>         self.redirect('/there')
>
>   Webapp's self.request is a WebOb request, so it is documented here:http://pythonpaste.org/webob/reference.html#id1
>
> Robert
>
>
>
>
>
>
>
> On Mon, Jan 17, 2011 at 12:52, Zeynel <azeyn...@gmail.com> wrote:
> > Hi,
>
> > I just added a new page "/hot" to sort by number of votes to articles.
>
> > VoteHandler handles the vote with
>
> > item.vote +=1
> > item.put()
> > self.redirect("/newest")
>
> > but now the new page "/hot" also refers to this VoteHandler.
>
> > After writing the new vote to the database I need to redirect to
> > either /hot or /newest depending on the referrer.
>
> > I searched the group and stackoverflow
> >http://stackoverflow.com/search?page=2&tab=relevance&q=redirect%20goo...

Robert Kluin

unread,
Jan 17, 2011, 2:32:54 PM1/17/11
to google-a...@googlegroups.com
Yep, you're right. The referrer is not relative, if the host part of
your request changes, remove it.

http://docs.python.org/release/2.5.2/lib/string-methods.html

Zeynel

unread,
Jan 17, 2011, 2:48:38 PM1/17/11
to Google App Engine
Ok, but not sure I see the "referrer" reference in the link you
supplied. I'll look again. Thanks.

Robert Kluin

unread,
Jan 17, 2011, 3:42:52 PM1/17/11
to google-a...@googlegroups.com
Its not, that is a link to the Python string method docs. You'll
probably want to use some of those when you're writing the code to
process the http referrer.


Robert

Nick Johnson (Google)

unread,
Jan 18, 2011, 11:27:03 PM1/18/11
to google-a...@googlegroups.com
I would strongly recommend avoiding relying on the 'referer' field for anything like this. It'll be unreliable - what if the user opened the vote page in a new tab? What if the user has referers disabled? Instead, pass the continue URL in to the handler, or have separate URLs based on the origin, or use AJAX, so the user doesn't have to leave the page they voted from in the first place.

-Nick Johnson

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To post to this group, send email to google-a...@googlegroups.com.
To unsubscribe from this group, send email to google-appengi...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine?hl=en.




--
Nick Johnson, Developer Programs Engineer, App Engine
Google Ireland Ltd. :: Registered in Dublin, Ireland, Registration Number: 368047

Zeynel

unread,
Jan 20, 2011, 12:06:27 PM1/20/11
to Google App Engine
On Jan 18, 11:27 pm, "Nick Johnson (Google)" <nick.john...@google.com>
wrote:

> ...
> pass the continue URL in to the handler,
> or have separate URLs based on the origin,
> or use AJAX ...

Thanks. I notice the problems that you mentioned when I test the app.
Can you help with some more specific references about how to implement
the methods you mention above?
Reply all
Reply to author
Forward
0 new messages