在Django视图中,您可以访问request.GET['variablename'],因此在您的视图中,您可以执行如下操作:myvar = request.GET['myvar']
实际的request.GET['myvar']对象类型是:
现在,如果要传递具有相同参数名的多个变量,即:
http://example.com/blah/?myvar=123&myvar=567
您希望为参数myvar返回pythonlist,然后执行以下操作:for var in request.GET['myvar']:
print(var)
但是,当您尝试只获取url中传递的最后一个值时,即在上面的示例中,您将获得567,shell中的结果将是:5
6
7
然而,当你打印request.GET时,它似乎有一个list即:
确定更新:
它的目的是返回最后一个值,我的用例是我需要一个列表。
来自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
KeyError
QueryDict.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.
更新:
如果有人知道django dev为什么这么做,请告诉我,显示一个列表似乎是违反直觉的,而且它的行为不像一个列表。不是很Python!