1 回答

慕虎7371278
TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超4個(gè)贊
字符型
字符型的數(shù)據(jù)相對(duì)好獲取,前端傳遞的方法如下:
sendData = {? "exporttype": exporttype,
"bugids": bugids,
"test": JSON.stringify({"test": "test"})
};
在Django的后端只要使用exporttype = request.GET.get("exporttype")
就能正常的獲取到這個(gè)數(shù)據(jù)了。
注意: 在Python2.7中數(shù)據(jù)是unicode編碼的,如果要使用,有時(shí)候需要進(jìn)行轉(zhuǎn)str
結(jié)果示例:
Excle
數(shù)組型
獲取數(shù)組型的數(shù)據(jù)如果使用獲取字符串的數(shù)據(jù)的方法,打出的結(jié)果是None。我們要使用這個(gè)方法:
bugids = request.GET.getlist("bugids[]")
這樣獲取的數(shù)據(jù)就是數(shù)組類(lèi)型。
注意: 獲取的數(shù)組中的元素是unicode編碼的,在某些時(shí)候使用需要轉(zhuǎn)編碼
結(jié)果示例:
?傳遞的url
[14/Jul/2016 11:00:41]"GET /testtools/exportbug/?exporttype=Excle&bugids;%5B%5D=102&bugids;%5B%5D=101&bugids;%5B%5D
?獲取的數(shù)據(jù)
[u'102', u'101', u'100', u'99', u'98', u'97', u'96', u'95', u'94', u'93', u'92', u'91', u'90', u'89', u'88', u'87'
字典型
字典型數(shù)據(jù)其實(shí)可以當(dāng)成字符串?dāng)?shù)據(jù)來(lái)處理,獲取到對(duì)應(yīng)字符串后使用JSON模塊做一下格式化就行了。
對(duì)于前端來(lái)說(shuō),傳遞字典型的數(shù)據(jù)就是傳遞JSON數(shù)據(jù),所以使用的方法是:
"test": JSON.stringify({"test": "test"})
結(jié)果示例:
{"test":"test"}
相關(guān)源碼
?Get方法
Get方法是wsgi里面的一個(gè)方法。
def GET(self):
# The WSGI spec says 'QUERY_STRING' may be absent.
raw_query_string = get_bytes_from_wsgi(self.environ, 'QUERY_STRING', '')
return http.QueryDict(raw_query_string, encoding=self._encoding)
最終返回的是一個(gè)http.QueryDict(raw_query_string, encoding=self._encoding)http的原始數(shù)據(jù),而QueryDict繼承于MultiValueDict ,所以我們直接看MultiValueDict就好了。
?MultiValueDict
其實(shí)源碼看起來(lái)并不難。
def get(self, key, default=None):
"""
Returns the last data value for the passed key. If key doesn't exist
or value is an empty list, then default is returned.
"""
try:
val = self[key]
except KeyError:
return default
if val == []:
return default
return val
def getlist(self, key, default=None):
"""
Returns the list of values for the passed key. If key doesn't exist,
then a default value is returned.
"""
try:
return super(MultiValueDict, self).__getitem__(key)
except KeyError:
if default is None:
return []
return default
def __getitem__(self, key):
"""
Returns the last data value for this key, or [] if it's an empty list;
raises KeyError if not found.
"""
try:
list_ = super(MultiValueDict, self).__getitem__(key)
except KeyError:
raise MultiValueDictKeyError(repr(key))
try:
return list_[-1]
except IndexError:
return []
字符型的數(shù)據(jù)相對(duì)好獲取,前端傳遞的方法如下:
sendData = {? "exporttype": exporttype,
"bugids": bugids,
"test": JSON.stringify({"test": "test"})
};
在Django的后端只要使用exporttype = request.GET.get("exporttype")
就能正常的獲取到這個(gè)數(shù)據(jù)了。
注意: 在Python2.7中數(shù)據(jù)是unicode編碼的,如果要使用,有時(shí)候需要進(jìn)行轉(zhuǎn)str
結(jié)果示例:
Excle
數(shù)組型
獲取數(shù)組型的數(shù)據(jù)如果使用獲取字符串的數(shù)據(jù)的方法,打出的結(jié)果是None。我們要使用這個(gè)方法:
bugids = request.GET.getlist("bugids[]")
這樣獲取的數(shù)據(jù)就是數(shù)組類(lèi)型。
注意: 獲取的數(shù)組中的元素是unicode編碼的,在某些時(shí)候使用需要轉(zhuǎn)編碼
結(jié)果示例:
?傳遞的url
[14/Jul/2016 11:00:41]"GET /testtools/exportbug/?exporttype=Excle&bugids;%5B%5D=102&bugids;%5B%5D=101&bugids;%5B%5D
?獲取的數(shù)據(jù)
[u'102', u'101', u'100', u'99', u'98', u'97', u'96', u'95', u'94', u'93', u'92', u'91', u'90', u'89', u'88', u'87'
字典型
字典型數(shù)據(jù)其實(shí)可以當(dāng)成字符串?dāng)?shù)據(jù)來(lái)處理,獲取到對(duì)應(yīng)字符串后使用JSON模塊做一下格式化就行了。
對(duì)于前端來(lái)說(shuō),傳遞字典型的數(shù)據(jù)就是傳遞JSON數(shù)據(jù),所以使用的方法是:
"test": JSON.stringify({"test": "test"})
結(jié)果示例:
{"test":"test"}
相關(guān)源碼
?Get方法
Get方法是wsgi里面的一個(gè)方法。
def GET(self):
# The WSGI spec says 'QUERY_STRING' may be absent.
raw_query_string = get_bytes_from_wsgi(self.environ, 'QUERY_STRING', '')
return http.QueryDict(raw_query_string, encoding=self._encoding)
最終返回的是一個(gè)http.QueryDict(raw_query_string, encoding=self._encoding)http的原始數(shù)據(jù),而QueryDict繼承于MultiValueDict ,所以我們直接看MultiValueDict就好了。
?MultiValueDict
其實(shí)源碼看起來(lái)并不難。
def get(self, key, default=None):
"""
Returns the last data value for the passed key. If key doesn't exist
or value is an empty list, then default is returned.
"""
try:
val = self[key]
except KeyError:
return default
if val == []:
return default
return val
def getlist(self, key, default=None):
"""
Returns the list of values for the passed key. If key doesn't exist,
then a default value is returned.
"""
try:
return super(MultiValueDict, self).__getitem__(key)
except KeyError:
if default is None:
return []
return default
def __getitem__(self, key):
"""
Returns the last data value for this key, or [] if it's an empty list;
raises KeyError if not found.
"""
try:
list_ = super(MultiValueDict, self).__getitem__(key)
except KeyError:
raise MultiValueDictKeyError(repr(key))
try:
return list_[-1]
except IndexError:
return []
- 1 回答
- 0 關(guān)注
- 1937 瀏覽
添加回答
舉報(bào)
0/150
提交
取消