Search code examples
pythongoogle-app-enginewebapp2

Run code before url mapping on google app engine


I'd like to check the datastore first, to see if there is any data, and if not, then redirect to another page (most likely /admin). However, I don't want to rewrite the url mapping framework that is already there.

Is there some way to set a handler that will process all requests before they are mapped?

I'm using google app engine with Python 2.7 and webapp2.


Solution

  • Yes, you can override dispatch() with a custom class. In the example shown in the link, the new class name is MyHandler. This means that all of your request classes need to be derived from MyHandler instead of webapp2.RequestHandler. Since this is how you would implement Sessions, you can put your code in dispatch() before it calls webapp2.RequestHandler.dispatch(self). In other words, you probably want to replace webapp2.RequestHandler anyway.