如何在 Django 中处理具有多个变量的同一参数的 request.GET
在 Django 视图中,您可以访问 request.GET['variablename']
,因此在您的视图中,您可以执行如下操作:
myvar = request.GET['myvar']
实际的 request.GET['myvar'] 对象类型为:
<class 'django.http.QueryDict'>
现在,如果您想传递具有相同参数名称的多个变量,即:
http://example.com/blah/?myvar=123&myvar=567
您会就像为参数 myvar
返回的 python list
一样,然后执行如下操作:
for var in request.GET['myvar']:
print(var)
但是,当您尝试时,您只能获取 url 中传递的最后一个值,即示例中的值上面你会得到567,shell中的结果将是:
5
6
7
但是,当你打印request.GET
时,它似乎有一个列表
即:
<QueryDict: {u'myvar': [u'123', u'567']}>
好的更新: 它旨在返回最后一个值,我的用例是我需要一个列表。
来自 Django 文档:
QueryDict.getitem(key) 退货 给定键的值。如果 key 有多个值, getitem() 返回最后一个值。提高 django.utils.datastructs.MultiValueDictKeyError 如果密钥不存在。 (这是一个 Python标准的子类 KeyError,所以你可以坚持捕获 按键错误
QueryDict.getlist(key) 返回 具有请求的密钥的数据,作为 Python 列表。如果返回空列表 密钥不存在。是有保证的 返回某种列表。
更新: 如果有人知道为什么 django 开发人员这样做,请告诉我,显示列表似乎违反直觉,而且它的行为也不像列表。不太Pythonic!
In a Django view you can access the request.GET['variablename']
, so in your view you can do something like this:
myvar = request.GET['myvar']
The actual request.GET['myvar']
object type is:
<class 'django.http.QueryDict'>
Now, if you want to pass multiple variables with the same parameter name, i.e:
http://example.com/blah/?myvar=123&myvar=567
You would like a python list
returned for the parameter myvar
, then do something like this:
for var in request.GET['myvar']:
print(var)
However, when you try that you only get the last value passed in the url i.e in the example above you will get 567, and the result in the shell will be:
5
6
7
However, when you do a print of request.GET
it seems like it has a list
i.e:
<QueryDict: {u'myvar': [u'123', u'567']}>
Ok Update:
It's designed to return the last value, my use case is i need a list.
from django docs:
QueryDict.getitem(key)
Returns
the value for the given key. If the
key has more than one value,
getitem() returns the last value. Raises
django.utils.datastructures.MultiValueDictKeyError
if the key does not exist. (This is a
subclass of Python's standard
KeyError, so you can stick to catching
KeyErrorQueryDict.getlist(key) Returns the
data with the requested key, as a
Python list. Returns an empty list if
the key doesn't exist. It's guaranteed
to return a list of some sort.
Update:
If anyone knows why django dev's have done this please let me know, seems counter-intuitive to show a list and it does not behave like one. Not very pythonic!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您想要 getlist() GET对象的功能:
You want the getlist() function of the GET object:
另一个解决方案是创建请求对象的副本...通常,您不能迭代 request.GET 或 request.POST 对象,但您可以对副本执行以下操作:
Another solution is creating a copy of the request object... Normally, you can not iterate through a request.GET or request.POST object, but you can do such operations on the copy:
当从包含同一参数的多个值(例如一组复选框)的 QueryDict 对象创建查询字符串时,请使用 urlencode() 方法:
例如,我需要获取传入的查询请求,删除参数并返回将查询字符串更新到结果页面。
现在,如果原始查询对同一参数有多个值,如下所示:
{...'track_id': ['1', '2'],...} 当使用如下代码时,您将丢失查询字符串中的第一个元素:
导致...
但是,可以使用 urlencode 方法QueryDict 类的以便正确包含多个值:
这会产生...
When creating a query string from a QueryDict object that contains multiple values for the same parameter (such as a set of checkboxes) use the urlencode() method:
For example, I needed to obtain the incoming query request, remove a parameter and return the updated query string to the resulting page.
Now if the original query has multiple values for the same parameter like this:
{...'track_id': ['1', '2'],...} you will lose the first element in the query string when using code like:
results in...
However, one can use the urlencode method of the QueryDict class in order to properly include multiple values:
which produces...