在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!