获得大量的Post值

时间:2013-12-02 16:13:01

标签: python tornado

class MergeHandler(tornado.web.RequestHandler):        
    def post(self):
        noun1 = self.get_argument('noun1')
        noun2 = self.get_argument('noun2')
        noun3 = self.get_argument('noun3')
        noun4 = self.get_argument('noun4')
        noun5 = self.get_argument('noun5')
        ...
        noun25 = self.get_argument('noun25')

        sys.exit()

我有多个输入,我不确切知道多少。即使我知道,真的很烦人

每次收到的每个输入都

nounX = self.get_argument('nounX')

有谁知道更好的方法吗?

3 个答案:

答案 0 :(得分:0)

当做这样的重复性任务时,几乎总是可以使用循环:

def post(self):
    NUM_NOUNS_EXPECTED = 25
    nouns = {}
    for i in range(1, NUM_NOUNS_EXPECTED + 1):
        key = 'noun{}'.format(i)
        nouns[key] = self.get_argument(key)

def post(self):
    required_params = ['something', 'again', 'one', 'two', 'three', 'smile', 'here']
    for param in required_params:
        self.get_argument(param)

答案 1 :(得分:0)

你可以试试这个

parameters = dict() 
for k in request.arguments.iterkeys(): 
  parameters[k] = self.get_argument(k, strip=False) 

来源:http://vmiroshnikov.com/post/33383858889/make-tornado-keep-blank-values-in-request-arguments

答案 2 :(得分:0)

def post(self):
    var_dump({ k: self.get_argument(k) for k in self.request.arguments })