[Twisted-web] HTTP redirection from data_*/render_* methods

Donovan Preston dp at ulaluma.com
Fri Jul 15 12:45:24 MDT 2005


On Jul 15, 2005, at 7:17 AM, <en.karpachov at ospaz.ru>  
<en.karpachov at ospaz.ru> wrote:

> On Fri, Jul 15, 2005 at 07:40:33AM -0600, Samuel Reynolds wrote:
>
>> At 2005-07-15 02:46 PM +0400, you wrote:
>>
>>> I wondered if I can make an HTTP redirection (302) from inside of  
>>> render_*
>>> or data_* method. Here is a code snipped I wrote; I'd like to  
>>> know if it
>>> can be considered as "good" nevow practice?
>>>
>>
>> What about
>>     request.redirect( redirectUrl )
>> possibly followed by
>>     request.finish()
>>
>
> It doesn't work when called from inside a render_something or  
> data_smth
> methods. If I redirect, I want the flattening process to stop  
> immediately,
> dropping request output accumulated so far. The only way to do it  
> is to
> raise an exception, cancelling normal flow.  Otherwise, flattening  
> will
> continue, and the request will be finished twice. Also, the http  
> reply will
> have non-empty body.

Even if you do raise an exception, all output generated up to that  
point will have already been sent out over the socket to the browser.  
This means the headers will already have been written, and  
redirecting will have no effect.

What you want instead is to set the "buffered" attribute of your Page  
class to true before it starts rendering. This will cause nevow to  
delay sending any output to the browser until the entire page is done  
rendering, allowing you to do a redirect in the middle of it.

Personally, I don't think it's a good idea. You should decide about  
the redirect up front, that way you avoid doing wasted work on the  
server. Buffering also isn't as cool, because pages will chunk in  
rather than streaming in.

Donovan




More information about the Twisted-web mailing list