我试图通过将POST变量提取和验证移到函数来干掉我的webapp2.RequestHandlers。
self.request.get在Handler外部的Function中不起作用。如何从函数中访问POST变量?
class my_form(webapp2.RequestHandler):
def post(self):
fieldvalidation('field1', 'string')
def fieldvalidation(fieldname, validationoptions):
x = self.request.get(fieldname) # <<< does not work outside of class my_form
.......
答案 0 :(得分:1)
你必须将self或self.request传递给函数。
class my_form(webapp2.RequestHandler):
def post(self):
fieldvalidation(self.request)
def fieldvalidation(request):
x = request.get(fieldname) # <<< get the fieldname from the request object