从Python Webapp2 App Engine中的函数访问self.request.get()

时间:2012-11-28 12:11:52

标签: google-app-engine webapp2

我试图通过将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
   .......

1 个答案:

答案 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