Python瓶子如何读取请求参数

2024-05-17 03:21:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用http://dingyonglaw.github.com/bootstrap-multiselect-dropdown/#forms来显示带有多个复选框的下拉列表。

<li>
  <label>
    <input type="checkbox" name="filters" value="first value">
    <span>First Value</span>
  </label>
</li>

<li>
  <label>
    <input type="checkbox" name="filters" value="second value">
    <span>Second Value</span>
  </label>
</li>

这是生成的URL:

http://example.com/search?filters=first+value&filters=second+value

在服务器端(瓶子):

terms = unicode (request.query.get ('filters', ''), "utf-8")

只给我“第二个值”而忽略“第一个值”。是否有收集所有“过滤器”值的方法?


Tags: namecomhttpinputvaluetypelifilters
2条回答

改用^{} method

FormsDict is a subclass of MultiDict and can store more than one value per key. The standard dictionary access methods will only return a single value, but the MultiDict.getall() method returns a (possibly empty) list of all values for a specific key.

嘿,我也有同样的问题,找到了解决办法

我将编写适用于您的问题的代码

HTML:(注意,我在这里不是很熟练,所以可能有错误,但基本结构是正确的)。在这里,我们要设置一个“表单操作”,并使用method=GET

<form action="/webpage_name" method="GET">
<li>
  <label>
    <input type="checkbox" name="filters" value="first value">
    <span>First Value</span>
  </label>
</li>
<li>
  <label>
    <input type="checkbox" name="filters" value="second value">
    <span>Second Value</span>
  </label>
</li>
<input type="submit" name="save" value="save">
</form> 

Python: 变量“all_filters”将从“filters”变量获取所有数据 从瓶子进口申请

@route('/webpage_name', method='GET')
def function_grab_filter():
    if request.GET.save:
        all_filters = request.GET.getall('filters')
        for ff in all_filters:
            fft = str(ff[0:]) # you might not need to do this but I had to when trying to get a number
            do soemthing....

相关问题 更多 >