亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

Django接受前端數(shù)據(jù)的幾種方法總結(jié)

asal 2017-01-09 14:16:35 429
abstrak:下面為大家?guī)硪黄狣jango接受前端數(shù)據(jù)的幾種方法總結(jié)。背景測試工具箱寫到一半,今天遇到了一個前后端數(shù)據(jù)交互的問題,就一起做一下整理。環(huán)境--------------------------------------------------------版本相關(guān)操作系統(tǒng):Mac OS X EI CaptionPython版本:2.7IDE:PyCharmDjango: 1.8.2----------

下面為大家?guī)硪黄狣jango接受前端數(shù)據(jù)的幾種方法總結(jié)。

背景

測試工具箱寫到一半,今天遇到了一個前后端數(shù)據(jù)交互的問題,就一起做一下整理。

環(huán)境

--------------------------------------------------------

版本相關(guān)

操作系統(tǒng):Mac OS X EI Caption

Python版本:2.7

IDE:PyCharm

Django: 1.8.2

---------------------------------------------------------

注: 我測試的是Get方法,POST方法也同樣適用

字符型

字符型的數(shù)據(jù)相對好獲取,前端傳遞的方法如下:

sendData = {?  "exporttype": exporttype,
?  "bugids": bugids,
?  "test": JSON.stringify({"test": "test"})
?};

在Django的后端只要使用exporttype = request.GET.get("exporttype")

就能正常的獲取到這個數(shù)據(jù)了。

注意: 在Python2.7中數(shù)據(jù)是unicode編碼的,如果要使用,有時候需要進行轉(zhuǎn)str

結(jié)果示例:

Excle <type 'unicode'>

數(shù)組型

獲取數(shù)組型的數(shù)據(jù)如果使用獲取字符串的數(shù)據(jù)的方法,打出的結(jié)果是None。我們要使用這個方法:

bugids = request.GET.getlist("bugids[]")

這樣獲取的數(shù)據(jù)就是數(shù)組類型。

注意: 獲取的數(shù)組中的元素是unicode編碼的,在某些時候使用需要轉(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ù)據(jù)來處理,獲取到對應(yīng)字符串后使用JSON模塊做一下格式化就行了。

對于前端來說,傳遞字典型的數(shù)據(jù)就是傳遞JSON數(shù)據(jù),所以使用的方法是:

"test": JSON.stringify({"test": "test"})

結(jié)果示例:

{"test":"test"} <type 'unicode'>

相關(guān)源碼

?Get方法

Get方法是wsgi里面的一個方法。

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)

最終返回的是一個http.QueryDict(raw_query_string, encoding=self._encoding)http的原始數(shù)據(jù),而QueryDict繼承于MultiValueDict ,所以我們直接看MultiValueDict就好了。

?MultiValueDict

其實源碼看起來并不難。

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 []

getlist方法也就是把數(shù)據(jù)全部整合一下,返回回來。

更多關(guān)于Django接受前端數(shù)據(jù)的幾種方法總結(jié)請關(guān)注PHP中文網(wǎng)(ipnx.cn)其他文章!

Nota Keluaran

Penyertaan Popular