Pyramid HTTP Redirect Usage

783 views
Skip to first unread message

Mark Erbaugh

unread,
Nov 22, 2011, 9:57:55 PM11/22/11
to pylons-discuss
In many of the Pyramid examples, that I've seen, the view callable will return a HTTP Redirect to send the user to a different page. Why doesn't the view callable just render and return that page directly? I just tried some test code and at least for my simplistic test, it seems to work.

I've seen the same thing in other web frameworks, so I'm guessing that it's a general web thing rather than something Pyramid-specific. Does it have to do with the displayed URL?


Thanks,
Mark

Eric Rasmussen

unread,
Nov 23, 2011, 12:55:09 AM11/23/11
to pylons-...@googlegroups.com
Hi Mark,

I believe this pattern came about because browsers will resubmit POST data if you don't issue a redirect after a POST request. If I submit a form on your website and you render the response without redirecting, then when I click "refresh" in the browser or advance one page and click the browser's back button, the POST request gets submitted again.

Most browsers warn users it will be resubmitted, but users don't always listen/understand, so issuing a redirect mitigates the problem.

Thanks,
Eric




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


Chris McDonough

unread,
Nov 23, 2011, 12:56:15 AM11/23/11
to pylons-...@googlegroups.com
On Tue, 2011-11-22 at 21:55 -0800, Eric Rasmussen wrote:
> Hi Mark,
>
> I believe this pattern came about because browsers will resubmit POST
> data if you don't issue a redirect after a POST request. If I submit a
> form on your website and you render the response without redirecting,
> then when I click "refresh" in the browser or advance one page and
> click the browser's back button, the POST request gets submitted
> again.
>
> Most browsers warn users it will be resubmitted, but users don't
> always listen/understand, so issuing a redirect mitigates the
> problem.

This is my understanding of the reasoning as well.

- C


>
> Thanks,
> Eric
>
>
>
> On Tue, Nov 22, 2011 at 6:57 PM, Mark Erbaugh <ma...@microenh.com>
> wrote:
> In many of the Pyramid examples, that I've seen, the view
> callable will return a HTTP Redirect to send the user to a
> different page. Why doesn't the view callable just render and
> return that page directly? I just tried some test code and at
> least for my simplistic test, it seems to work.
>
> I've seen the same thing in other web frameworks, so I'm
> guessing that it's a general web thing rather than something
> Pyramid-specific. Does it have to do with the displayed URL?
>
>
> Thanks,
> Mark
>
> --
> You received this message because you are subscribed to the
> Google Groups "pylons-discuss" group.
> To post to this group, send email to
> pylons-...@googlegroups.com.

> To unsubscribe from this group, send email to pylons-discuss
> +unsub...@googlegroups.com.


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

> To unsubscribe from this group, send email to pylons-discuss
> +unsub...@googlegroups.com.

Christoph Zwerschke

unread,
Nov 23, 2011, 3:19:39 AM11/23/11
to pylons-...@googlegroups.com
Am 23.11.2011 06:55, schrieb Eric Rasmussen:
>> I believe this pattern came about because browsers will resubmit POST
> data if you don't issue a redirect after a POST request. If I submit a
> form on your website and you render the response without redirecting,
> then when I click "refresh" in the browser or advance one page and click
> the browser's back button, the POST request gets submitted again.

It's also known as the PRG pattern:
http://en.wikipedia.org/wiki/Post/Redirect/Get

-- Christoph

henninge

unread,
Nov 23, 2011, 3:50:26 AM11/23/11
to pylons-...@googlegroups.com
The reasons for redirecting depend on what the view is doing. The "resend-data-on-POST" problem was already mentioned but then the view could take other measures to prevent information from being submitted twice.

There is also the reason of URL consistency and hygiene. A URL (or URI to be precise), by definition, is meant to represent exactly one specific document on the web. It is not a good idea to put two or more different documents in the same place, or have the same document accessible on two different URLs. Of course, the definition of what different documents are depend on the application. So, instead of rendering the same document in two places, have one canonical place and redirect there.

Also, it might be interesting to look at the different kinds of redirection that HTTP offers (3xx status codes). http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Henning

JPe...@ykksnap-america.com

unread,
Nov 23, 2011, 11:10:20 AM11/23/11
to pylons-...@googlegroups.com
A couple of more points:  
the first time I saw this pattern was at
http://www.theserverside.com/news/1365146/Redirect-After-Post

This is either the primary source, or very near it, and
is incredibly well-written.  It is cited in the wikipedia
article, but this version is better.

It is very common to use renderers in views.  After
a post, you will want to return something to the user,
usually NOT a blank version of the form they just filled
in.  So, you are going to have to do something special.
Your choices are pretty much limited to render_to_response or
HTTPFound.   HTTPFound is no more work than render_to_response
and has the additional benefits outlined in the article,
so, there is usually little reason not to use the pattern.

 

Raj

unread,
Jan 15, 2012, 4:35:07 AM1/15/12
to pylons-discuss
Reply all
Reply to author
Forward
0 new messages