python - How to create URL Parameters from a list -


i have form multiple select field. working through get method. example of request parameters generated form:

action=not-strummed&action=not-rewarded&keywords=test&page=2 

note there 2 "action" parameters. happening because of multiple select.
want is:

  • make dict parameters
  • remove "page" key dict
  • transform-back dict parameter string

the urllib.urlencode() isn't smart enough generate url parameters list.

for example:

{      "action": [u"not-strummed", u"not-rewarded"] } 

urllib.urlencode() transforms dict as:

action=%5bu%27not-strummed%27%2c+u%27not-rewarded%27%5d 

this wrong , useless.

that's why wrote iteration code re-generate url parameters.

parameters_dict = dict(self.request.get.iterlists()) parameters_dict.pop("page", none) pagination_parameters = "" key, value_list in parameters_dict.iteritems():     value in value_list:         pagination_item = "&%(key)s=%(value)s" % ({             "key": key,             "value": value,         })         pagination_parameters += pagination_item 

it working well. doesn't cover possibilities , not pythonic.

do have better (more pythonic) idea creating url parameters list?

thank you

you should able use second doseq parameter urlencode offers:

http://docs.python.org/2/library/urllib.html

so basically, can pass dictionary of lists urlencode so:

urllib.urlencode(params, true) 

and right thing.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -