[Zope] How to redirect in ZPT.

J Cameron Cooper jccooper at jcameroncooper.com
Tue Feb 3 18:43:06 EST 2004


Jason C. Leach wrote:

>So I have a DTML I would like to convert to a ZPT.  Its name is index_html
>and does this:
><dtml-call expr="RESPONSE.redirect(absolute_url() + '/' +
>REQUEST['AUTHENTICATED_USER'].getUserName())">
>
>The problem with this is I get a URL like:
>http://www.foo.com/clients/index_html/myClient/someresource
>
>And I can end up with URLs like:
>http://www.foo.com/clients/index_html/myClient/bla/myClients/yyy/myClients/zzzz
>
>and on and on and on. It works but the URL gets very long.
>
>
>I would like that in a ZPT and was thinking:
>
><metal:foo tal:define="home user/getUserName">
>  <html tal:content="here/clients/${home}/index_html"> </html>
></metal:foo>
>
>but that does not work. I'm not sure how to get it to.
>
The request is bound to 'request' in TAL. So::

<tal:call tal:define="dummy 
python:request.RESPONSE.redirect('/clients/'+user.getUserName())" />

Or something similar. Probably should be a script, though, since doing a 
redirect either means

 - you're doing logic, which is better kept out of display templates, or
 - the page will never be seen, in which case it's unnecessary (and a 
bit wasteful) to use a page template

The reason you get bad recursion in your URLs, btw, is because you're 
tacking the new destination on to the current location. You either need 
to get the 'absolute_url' of the site root or use root-based paths like 
I did above. (But that's not a valid strategy for getting to relative 
places: that requires a different technique.)

          --jcc

-- 
"Code generators follow the 80/20 rule. They solve most of the problems, but not all of the problems. There are always features and edge cases that will need hand-coding. Even if code generation could build 100 percent of the application, there will still be an endless supply of boring meetings about feature design."
(http://www.devx.com/java/editorial/15511)




More information about the Zope mailing list