Is it possible to get values from query string with same name?

自闭症网瘾萝莉.ら 提交于 2019-12-10 17:18:17

问题


I want to know if it's possible to get values from this query string?

'?agencyID=1&agencyID=2&agencyID=3'  

Why I have to use a query string like this?
I have a form with 10 check boxes. my user should send me ID of news agencies which he/she is interested in. so to query string contains of multiple values with the same name. total count of news agencies are variable and they are loaded from database.
I'm using Python Tornado to parse query strings.


回答1:


Reading the tornado docs, this seems to do what you want

RequestHandler.get_arguments(name, strip=True)

Returns a list of the arguments with the given name. If the argument is not present, returns an empty list. The returned values are always unicode.

So like this

ids = self.get_arguments('agencyID')

Note that here i used get_arguments, there is also a get_argument but that gets only a single argument.


You can get the whole query with

query = self.request.query



回答2:


>>> from urlparse import urlparse, parse_qs
>>> url = '?agencyID=1&agencyID=2&agencyID=3' 
>>> parse_qs(urlparse(url).query)
{'agencyID': ['1', '2', '3']}



回答3:


tornado.web.RequestHandler has two methods to get arguments:

  • get_argument(id), which will get the last argument with name 'id'.
  • get_arguments(id), which will get all arguments with name 'id' into a list even if there is only one.

So, maybe get_arguments is what you need.



来源:https://stackoverflow.com/questions/24030565/is-it-possible-to-get-values-from-query-string-with-same-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!