June 28, 2007, at 12:54 PM
Tips /

CherryPyRedirect

I have been playing with CherryPy and struggled with redirecting a page to another. I looked at the doc and search the site for info but found a definite answer on .

You have to raise an HTTPRedirect exception. The exception is not there to signal an error but to break the execution flow (it bothered me at first but ...).

import cherrypy

class WebApp:

    @cherrypy.expose
    def index(self):
        raise cherrypy.HTTPRedirect(cherrypy.url('/redirected'))

    @cherrypy.expose
    def redirected(self):
        return "Hello <em>redirected</em> world!"

if __name__ == "__main__":
    cherrypy.quickstart(WebApp())