web.redirect vs web.seeother

216 views
Skip to first unread message

gregp...@gmail.com

unread,
Sep 6, 2007, 11:06:47 PM9/6/07
to web.py
Hi kind web.py folks,

Just a fundamentals type question. When should I use web.redirect and
when should I use web.seeother?

What are the differences? How do they affect caching?

Thanks,

Greg

Gary Bernhardt

unread,
Sep 6, 2007, 11:30:00 PM9/6/07
to we...@googlegroups.com
On 9/6/07, gregp...@gmail.com <gregp...@gmail.com> wrote:
> Just a fundamentals type question. When should I use web.redirect and
> when should I use web.seeother?
>
> What are the differences? How do they affect caching?

web.redirect is HTTP 301, which means "the thing you're looking for
has moved and is now permanently at this other location".
web.seeother is HTTP 303, which means "the result of your request is
at this other URL", which is a temporary kind of redirect. The major
practical difference is that the result of a 301 should be cached by
clients, but 303 shouldn't. You can find more complete information in
the standard [1]. Of course, as always, the various browsers may not
implement these status codes correctly.

[1] http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

--
Gary
http://blog.extracheese.org

gregp...@gmail.com

unread,
Sep 7, 2007, 12:35:45 AM9/7/07
to web.py
On Sep 6, 11:30 pm, "Gary Bernhardt" <gary.bernha...@gmail.com> wrote:
> web.redirect is HTTP 301, which means "the thing you're looking for
> has moved and is now permanently at this other location".
> web.seeother is HTTP 303, which means "the result of your request is
> at this other URL", which is a temporary kind of redirect. The major
> practical difference is that the result of a 301 should be cached by
> clients, but 303 shouldn't. You can find more complete information in
> the standard [1]. Of course, as always, the various browsers may not
> implement these status codes correctly.
>
> [1]http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
>

So it seems like it would be rare to want to use web.redirect,
correct?

Does this mean this authentication example is using it incorrectly?
http://webpy.infogami.com/authentication
Relevent code:
if inp.username == user.username and inp.password ==
user.password:
dologin(user)
web.redirect('/')
else:
web.render('login.html')

(BTW side comment, I don't think that page made it over to the new
wiki)

-Greg

Ben Hoyt

unread,
Sep 7, 2007, 3:52:51 AM9/7/07
to we...@googlegroups.com
Good answer from Gary -- cheers.

So it seems like it would be rare to want to use web.redirect,
correct?

Yeah, I guess rarer than 303. We use it in our code for redirecting people to the canonical URLs. For instance, if you visit www.micropledge.com , you'll get a 301 (web.redirect) pointing you to micropledge.com. And if you visit http://micropledge.com/projects/MoDWsGi you'll get a 301 pointing you to http://micropledge.com/projects/modwsgi

You could just serve up the right page without the 301. But having canonical URLs is A Good Thing, search engines like it, and you get nice consistent URLs floating around the place.

Does this mean this authentication example is using it incorrectly?
http://webpy.infogami.com/authentication
Relevent code:
        if inp.username == user.username and inp.password ==
user.password:
            dologin(user)
            web.redirect('/')
        else:
            web.render('login.html')

Yeah, for redirect-after-POST I think it should be using web.seeother (HTTP 303) for that. From the spec: "This method exists primarily to allow the output of a POST-activated script to redirect the user agent to a selected resource."

Cheers,
Ben.

--
Ben Hoyt, +64 21 331 841
http://www.benhoyt.com/

gregp...@gmail.com

unread,
Sep 7, 2007, 11:12:13 AM9/7/07
to web.py
On Sep 7, 3:52 am, "Ben Hoyt" <benh...@gmail.com> wrote:
> Does this mean this authentication example is using it incorrectly?
>
> >http://webpy.infogami.com/authentication
> > Relevent code:
> > if inp.username == user.username and inp.password ==
> > user.password:
> > dologin(user)
> > web.redirect('/')
> > else:
> > web.render('login.html')
>
> Yeah, for redirect-after-POST I think it should be using web.seeother (HTTP
> 303) for that. From the
> spec<http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.4>:

> "This method exists primarily to allow the output of a POST-activated script
> to redirect the user agent to a selected resource."
>

Thanks, Ben. Well then, I think this is the cause of the not
reloading after login problem you mentioned. Well, I"m hoping at
least. I'll try it out later. And getting rid of my inappropriate
redirects is the right thing to do anyway :-)

Group, FYI Ben is having an issue on my site (utilitymill.com) where
after he logs in, the home page doesn't change/update to reflect the
fact that he's logged in until he does a refresh. I'm suspecting it's
the redirect so far.

-Greg

Reply all
Reply to author
Forward
0 new messages