当 GET 和 POST 方法都在同一个处理程序类中,并且我想在表单验证失败后使用用户输入填充表单字段时,我这样做......
Class CommentHandler(BaseHandler):
def get(self, form=None):
if form is None: # create new form unless populated form is passed in
form = CommentForm()
# query DB and create template context
self.render('page.html', **context)
def post(self):
form = CommentForm(self.request.POST)
if form.validate():
# populate entity with form data and save to DB
return self.redirect_to('page')
self.get(form=form) # pass populated form back to user for editing
我不知道这是否是将表单数据返回给用户的最佳方式,但它似乎有效。我的问题是:如果 GET 和 POST 方法位于不同的处理程序类中,我如何将该数据传递回表单?
class PageHandler(BaseHandler):
def get(self):
# displays form to user
class CommentHandler(BaseHandler):
def post(self):
# processes POSTed form data...
# but if form.validate() fails,
# how can I pass the form data back to the user
# so they can edit their form input?