Redirect alert msg

115 views
Skip to first unread message

GaneshPandi

unread,
Aug 20, 2012, 3:04:19 AM8/20/12
to cherryp...@googlegroups.com
Hi...
                         In my application  am using cherrypy decorators to provide auth , i am redirecting to some path if the user not having access.....but i want to display some msg, while redirect....
                         how i can do this...  pls help me friends... Thanks in advance


---G.GaneshPandi---

Eric Larson

unread,
Aug 20, 2012, 1:27:09 PM8/20/12
to cherryp...@googlegroups.com
That sounds like you want to return some HTML with some Javascript that
will display the message and perform the redirect. For example,
something like this:

<html><head><title>Redirecting...</title>
<script type="text/javascript">
setTimeout(5000, "document.location = '/login'");
</script>
</head>
<body>
You are not authorized to view this page. Redirecting you to a login
page... <a href="/login">Click here</a> if you are not redirected
automatically.
</body></html>

That said, I'd personally forego the redirect HTML page as it requires
your client to understand Javascript. While most clients handle
javascript, all clients should handle the HTTP 303 status and redirect
properly. It is also easier to test (IMO) because you can verify the
response is a 303 vs. parsing the HTML for some javascript or text.

HTH,

Eric




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

GaneshPandi

unread,
Aug 21, 2012, 8:56:40 AM8/21/12
to cherryp...@googlegroups.com
Thanks Eric....
ur answer is helpful..

but i want in server side itself, her is my code
                   if not condition:
                        raise cherrypy.HTTPRedirect("/foo")
                 
Here what i want is, before redirect i have to delay and provide some alert, in python(cherrypy) itself(no javascript)
--
---G.GaneshPandi---

Walter Woods

unread,
Aug 21, 2012, 11:23:54 AM8/21/12
to cherryp...@googlegroups.com
If you're going through the cherrypy logs for your server proces, you could always issue something like:

cherrypy.log("Redirecting a client to /foo!")

But most systems that do this type of server-side notification would send an e-mail to you to let you know something happened, or if you're counting it as a statistic, log it to a database somewhere.

Hope that helps,
Walt

Eric Larson

unread,
Aug 21, 2012, 11:52:11 AM8/21/12
to cherryp...@googlegroups.com

GaneshPandi <guruga...@gmail.com> writes:

> Thanks Eric....
> ur answer is helpful..
>
> but i want in server side itself, her is my code
> if not condition:
> raise cherrypy.HTTPRedirect("/foo")
>
> Here what i want is, before redirect i have to delay and provide some alert, in
> python(cherrypy) itself(no javascript)
>

I'm not sure what you mean by "alert". When you return a redirect (like
cherrypy.HTTPRedirect) the browser doesn't typically display any content
in the response. This is true of HTTP and is not specific to
cherrypy. This means if you want the user to have some sort of delay and
message, you'd have to do something like I mentioned before.

The reason this is the case is because a cherrypy.HTTPRedirect is like:

cherrypy.response.headers['Location'] = '/foo'
raise cherrypy.HTTPError(303)

The browser gets a response like this:

303 See Other
Location: /foo

When the browser gets the response it doesn't display the response body
b/c HTTP says to redirect to the URL found in the Location header.

Hope that helps clarify things a bit.

Eric

Eric Larson

unread,
Aug 21, 2012, 12:33:10 PM8/21/12
to cherryp...@googlegroups.com
Here is what shows up in the logs when you return a 303 in the browser.

127.0.0.1 - - [21/Aug/2012:11:26:21] "GET /redirect HTTP/1.1" 303 90 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1"
127.0.0.1 - - [21/Aug/2012:11:26:21] "GET / HTTP/1.1" 200 12 "" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:14.0) Gecko/20100101 Firefox/14.0.1"

The effect in the browser is as if you requested '/' directly.

Here is the code:

import cherrypy

class Root(object):
@cherrypy.expose()
def index(self):
return 'Hello World!'

@cherrypy.expose()
def redirect(self):
raise cherrypy.HTTPRedirect('/')


if __name__ == '__main__':
cherrypy.tree.mount(Root(), '/')
cherrypy.engine.start()
cherrypy.engine.block()
# visit http://localhost:8080/redirect

That is not to say you can't or shouldn't log extra info for
redirects. I'm just pointing out the default logs already contain
information on the redirect.

HTH,

Eric
Reply all
Reply to author
Forward
0 new messages