redirect after POST problem

879 views
Skip to first unread message

Reikje

unread,
May 31, 2012, 4:44:54 AM5/31/12
to python-...@googlegroups.com
Hi,

in one of my RequestHandlers I want to do a redirect after POST.

class CategoryHandler(tornado.web.RequestHandler):

    def post(self, *args, **kwargs):
        # some logic here left out
        self.redirect("/admin/", permanent=True)

Currently Iam running this code from a AsyncHTTPTestCase. The redirect is also done using POST which is a bit strange, isn't this supposed to be a GET? The problem is that the RequestHandler receiving the redirect doesn't normally handle posts. Here is some output:

INFO:root:301 POST /admin/category (127.0.0.1) 0.66ms
WARNING:root:404 POST /admin/ (127.0.0.1) 1.49ms

Alek Storm

unread,
May 31, 2012, 2:17:35 PM5/31/12
to python-...@googlegroups.com
You're using the wrong response code - 301 tells the browser that the script itself can be found elsewhere, and to retry the POST at that location. 303 See Other was specifically designed for redirecting users after the completion of a POST (maybe we should mention this in the docs). Use it with:

self.redirect("/admin/", status=303)

Alek

Ben Darnell

unread,
Oct 15, 2012, 6:32:31 PM10/15/12
to python-...@googlegroups.com
In practice, 302 and 303 work the same way, and there are some doubts
(but no solid data) about older browsers and proxies having problems
with 303 and 307 (which weren't introduced until http 1.1; see
https://code.djangoproject.com/ticket/13277 and
http://news.ycombinator.com/item?id=2791663).

I don't like the idea of redirect() changing behavior depending on the
request method. Also note that while it's common to use a 303
redirect after a POST (so that the next page is a GET and doesn't
cause problems with form re-submissions on reload), other redirect
codes like 301 and 307 are perfectly valid for POSTs (if you want to
tell the browser to make the same POST elsewhere).

-Ben

On Mon, Oct 15, 2012 at 2:51 PM, Troy Bollinger
<troy.bo...@gmail.com> wrote:
> I was under the impression that a redirect from a POST should always use
> 303. Is there a reason why that isn't self.redirect()'s default status for
> POSTs?
>
> Troy
Reply all
Reply to author
Forward
0 new messages