[Zope] newbie question: Redirect from Python?

Michel Pelletier michel@digicool.com
Mon, 27 Nov 2000 13:19:30 -0800


Paul Winkler wrote:
> 
> seb bacon wrote:
> >
> > * Paul Winkler <slinkp23@yahoo.com> [001127 19:54]:
> > > I want the user to be sent to a particular URL after calling the
> > > product's manage_add method. How can I do that?
> > > The URL I want is REQUEST['URL3'].
> > >
> > > I've tried RESPONSE.redirect(REQUEST['URL3']) but that's not it - the
> > > RESPONSE doesn't seem to exist here.
> >
> > sounds alright to me.  are you calling it in quotes, e.g. <dtml-var
> > "RESPONSE.redirect(REQUEST['URL3'])"> ?
> >
> > you shouldn't have to use the REQUEST to look up the variable, either:
> > <dtml-var "RESPONSE.redirect(URL3)">
> 
> Thanks for the reply.
> OK, I didn't phrase the question correctly.
> 
> I think it boils down to this:
> If I have a string that represents a URL, can I define a method of my
> product (in python, not in DTML) that redirects the browser to that
> URL?
> 
> More details:
> 
> My product code is based on Boring (like most newbies I guess).
> But manage_addBoring() does not do quite what I want. It returns
> self.manage_main(self, REQUEST), so they end up at a long and ugly URL
> that isn't where I want them to be anyway.
> 
> Example:
>  the user is at:
> 
> http://localhost.localdomain:8080/Members/bobby
> 
> Bobby then clicks on a button I've provided that says "Add Test
> Product". That sends him to the add form I've created. When Bobby
> submits the request from this form, the product is added and he's sent
> to
> 
> http://localhost.localdomain:8080/Members/bobby/manage_addProduct/MyProduct/manage_main
> 
> ...because manage_addMyProduct returns self.manage_main
> So I understand what's happening, but it's not what I want.
> I don't want to send him there, I want to send him back to
> 
> http://localhost.localdomain:8080/Members/bobby
> 
> So how can I do that from within the manage_addMyProduct method
> definition?
> The method gets a REQUEST argument, and I know that REQUEST['URL3'] is
> what I want because if I return that string, it prints the URL I want.
> 
> I could do it by returning an HTML page with a redirect in the <head>
> but that seems like a horribly ugly solution.
> 
> Looking through various Zope docs, I thought RESPONSE.redirect might
> work, but  RESPONSE is not mentioned anywhere in Boring.py. Boring
> only uses REQUEST.

So pass it RESPONSE:

def manage_addMyProduct(self, blah, blah, REQUEST=None, RESPONSE=None):
  ...

The publisher will automaticaly pass you RESPONSE if you ask for it as a
method argument.  Now use RESPONSE.redirect.

-Michel