Posting from one RequestHandler to another in App Engine
Goal: Using app engine's basic webapp framework I want to create a new request, with post data, to send to another RequestHandler. Something like pageGenerator.post({'message':'the message','datum1':datum1,...})
...
Problem Description: One request handler, call it pageGenerator
, creates a page with a form on it. When the user submits the form, the post goes to a different handler: dat开发者_Python百科aProcessor
. If dataProcessor
finds some problem with the submitted data it would send the submitted data plus an Error Message to 'pageGenerator`'s post method, and pageGenerator would serve up the page with the error message.
How do I pass data (and control) back and forth like this? I would like pageGenerator
to be able to get the data with self.request.get('message')
.
Sounds like you're over-complicating things. Consider just having a common method to show the form that can be invoked in different circumstances:
class FormHandler(webapp.RequestHandler):
def get(self):
self.show_form()
def post(self):
if form_is_valid():
handle_success()
else:
self.show_form({'feedback':'Validation failed'})
def show_form(self, vals={}):
vals['field1'] = self.request.get('field1')
vals['field2'] = self.request.get('field2')
html = template.render('form.html', vals)
self.response.out.write(html)
If you really need "display form" and "process form" to be in different handler classes, you can accomplish the same thing by defining show_form()
in a common parent class.
精彩评论