How to get multiple parameters with same name from a URL in Pylons?

2024/10/5 11:17:43

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"]

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')])

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

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

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?

Answer

From the docs - http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/glossary.html#term-multidict :

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.

So just call:

request.params.getall('c')
https://en.xdnf.cn/q/70487.html

Related Q&A

Kivy: Access configuration values from any widget

Im using kivy to create a small App for computer aided learning.At the moment I have some problems with accessing config values. I get the value withself.language = self.config.get(basicsettings, langu…

Multiprocessing with threading?

when I trying to make my script multi-threading, Ive found out multiprocessing,I wonder if there is a way to make multiprocessing work with threading?cpu 1 -> 3 threads(worker A,B,C) cpu 2 -> 3 …

Pandas Groupby Unique Multiple Columns

I have a dataframe.import pandas as pd df = pd.DataFrame( {number: [0,0,0,1,1,2,2,2,2], id1: [100,100,100,300,400,700,700,800,700], id2: [100,100,200,500,600,700,800,900,1000]})id1 id2 nu…

OpenCV Error: Assertion failed when using COLOR_BGR2GRAY function

Im having a weird issue with opencv. I have no issues when working in a jupyter notebook but do when trying to run this Sublime.The error is: OpenCV Error: Assertion failed (depth == CV_8U || depth == …

matplotlib 1.3.1 has requirement numpy=1.5, but youll have numpy 1.8.0rc1 which is incompatible

Im executing bellow command in Mac (High Sierra) as a part of getting started with pyAudioAnalysis.pip install numpy matplotlib scipy sklearn hmmlearn simplejson eyed3 pydub Im getting following error…

VS Code Debugger Immediately Exits

I use VS Code for a python project but recently whenever I launch the debugger it immediately exits. The debug UI will pop up for half a second then disappear. I cant hit a breakpoint no matter where i…

Sudoku Checker in Python

I am trying to create a sudoku checker in python:ill_formed = [[5,3,4,6,7,8,9,1,2],[6,7,2,1,9,5,3,4,8],[1,9,8,3,4,2,5,6,7],[8,5,9,7,6,1,4,2,3],[4,2,6,8,5,3,7,9], # <---[7,1,3,9,2,4,8,5,6],[9,6,1,5,…

Subclassing numpy scalar types

Im trying to subclass numpy.complex64 in order to make use of the way numpy stores the data, (contiguous, alternating real and imaginary part) but use my own __add__, __sub__, ... routines.My problem i…

Python file IO w vs wb [duplicate]

This question already has answers here:What does wb mean in this code, using Python?(5 answers)Closed 10 years ago.Wondering what the real difference is when writing files from Python. From what I can…

How to extract hour, minute and second from Series filled with datetime.time values

Data:0 09:30:38 1 13:40:27 2 18:05:24 3 04:58:08 4 09:00:09Essentially what Id like to do is split this into three columns [hour, minute, second]Ive tried the following code but none see…