[Zope] Redirect to a specific page
Danny William Adair
danny@adair.net
Tue, 16 Oct 2001 09:17:02 +1300
Hi Konstantinos!
To redirect to a specific page, you do
<dtml-call
"RESPONSE.redirect(maybe_some_path_in_a_variable+'maybe_some_string_literal')">
in DTML, and in a Python Script
context.REQUEST.RESPONSE.redirect(maybe_some_path_in_a_variable+'maybe_some_string_literal')
If you want to redirect to a certain page when a user tries to access a
non-existing resource, the easiest and most elegant approach IMHO would be to
put a redirect into the standard_error_message, there you can check for a 404
and bring the client to whatever you think is a suitable standard page. For
example, you could do something like this:
<dtml-if "error_type=='NotFound'">
<dtml-call "RESPONSE.redirect(Home.absolute_url())">
</dtml-if>
For more information on the standard_error_message and how to customize (most
of the) error handling, see John Chandler's HowTo:
http://www.zope.org/Members/JohnC/StandardErrorMessage
Remember that since you can create standard_error_message objects further
down your tree, you can redirect to different pages depending on the location
of the last successful traversal step. In you your root folder, you could have
"Sorry, the page you requested could not be found"
while a standard_error_message in a subfolder "Members" could return
"Sorry, this Member page could not be found. Please check the username"
(bit silly, couldn't think of anything better right now)
And since you always acquire from where you are, you can do a lot more tricks
like having only one standard_error_message in the root folder, but taking
the error_message - or even the URL to redirect to! - from a folder property:
<dtml-call "RESPONSE.redirect(redirect_url_404)">
Whereever you want to redirect to a specific URL, you would now just create a
folder property "redirect_url_404" (string) holding the url, and all request
to non-existing resources inside that folder (and further down, until you
redefine "redirect_url_404") will be redirected to that location.
Another trick could be to let standard_error_message be a DTML Document
instead of a DTML Method, then it always works in its _own_ namespace,
not the caller's namespace. But IMHO this has more drawbacks than advantages.
hth,
Danny
On Tuesday 16 October 2001 08:39, Konstantinos Margaritis wrote:
> Hi,
> Is it possible to redirect to a specific page no matter what the user
> asks for? I mean the browser could request the url
>
> http://foo.bar.com/luser and I would redirect it to
> http://foo.bar.com/template?username=luser
>
> The problem is that when I request a URL that doesn't exist it reports a
> 'Not found' error.