如何从Pylons中的URL获取具有相同名称的多个参数? [英] How to get multiple parameters with same name from a URL in Pylons?

查看:136
本文介绍了如何从Pylons中的URL获取具有相同名称的多个参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,不幸的是,我发现自己需要修改现有的Pylons应用程序以处理提供多个具有相同名称的参数的URL.类似于以下内容... domain:port/action?c = 1& v = 3& c = 4

So unfortunately I find myself in the situation where I need to modify an existing Pylons application to handle URLs that provide multiple parameters with the same name. Something like the following... domain:port/action?c=1&v=3&c=4

按常规方式可以通过这种方式访问​​参数...

Conventionally the parameters are accessed this way...

from pylons import request
c = request.params.get("c")
#or
c = request.params["c"]

在两种情况下,这都将返回"4"作为值,因为在这些情况下,忽略最后一个值之外的所有值似乎是标准行为.我真正需要的是能够同时访问这两者.我尝试打印出request.params并得到了类似的东西...

This will return "4" as the value in either case, because ignoring all but the last value seems to be the standard behavior in these situations. What I really need though, is to be able to access both. I tried printing out request.params and get something like this...

NestedMultiDict([(u'c', u'1'),(u'v', u'3'),(u'c', u'4')])

我还没有找到索引或访问c的第一个值的方法.

I haven't found a way to index into it, or access that first value for c.

我找到了与此问题相关的类似问题,但使用PHP可以解决:

I found a similar question relating to this problem, but solved with PHP:

按照这些思路进行工作对我来说很好,但是也许一些适合Pylons的Python代码.以前有没有人处理过类似的事情?

Something along these lines would work well for me, but maybe some Python code that would fit into Pylons. Has anyone dealt with something like this before?

推荐答案

从文档开始-

multidict一个有序词典,每个键可以有多个值.将方法getall,getone,mixed,add和dict_of_lists添加到常规词典界面中.请参阅Multidict和pyramid.interfaces.IMultiDict.

multidict An ordered dictionary that can have multiple values for each key. Adds the methods getall, getone, mixed, add and dict_of_lists to the normal dictionary interface. See Multidict and pyramid.interfaces.IMultiDict.

所以只需致电:

request.params.getall('c')

这篇关于如何从Pylons中的URL获取具有相同名称的多个参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆