Django accepts a summary of several methods for front end data

  • 2020-05-17 05:46:13
  • OfStack

background

The test toolkit has been written for 1 and a half. Today, I encountered a problem of data interaction between the front and back end, so I sorted it out for 1 time.

The environment

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

Version of the relevant

Operating system: Mac OS X EI Caption

Python version: 2.7

IDE: PyCharm

Django: 1.8.2

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

Note: I tested the Get method, and the POST method works as well

character

Character data is relatively easy to get, the front-end transfer method is as follows:


sendData = {
  "exporttype": exporttype,

  "bugids": bugids,

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

};

At the back end of Django, simply use exporttype = request.GET.get ("exporttype")

You can get the data normally.

Note: in Python2.7, the data is encoded by unicode. If you want to use Python2.7, sometimes you need to transfer to str

Examples of results:

Excle < type 'unicode' >

The array type

Get array data if you use string data method, the output is None. We will use this method:

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

The data retrieved in this way is of type array.

Note: the elements in the retrieved array are unicode encoded, which requires transcoding at some point

Examples of results:

The & # 8226; The passed url


[14/Jul/2016 11:00:41]"GET /testtools/exportbug/?exporttype=Excle&bugids%5B%5D=102&bugids%5B%5D=101&bugids%5B%5D

The & # 8226; Acquired data


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

Word is a typical

In fact, the word typical data can be treated as string data. After obtaining the corresponding string, JSON module is used to do 1 formatting.

For the front-end, the typical data of the transfer word is JSON data, so the method used is:

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

Examples of results:

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

Related to the source code

The & # 8226; Get method

The Get method is one of the methods in 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, QueryDict inherits from MultiValueDict, so let's just look at MultiValueDict.

•MultiValueDict

In fact, the source code does not look difficult.


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

The getlist method is to put all the data together 1 time and go back.


Related articles: