Home  >  Article  >  Backend Development  >  Django parses Angular's POST data

Django parses Angular's POST data

高洛峰
高洛峰Original
2017-02-18 10:36:091307browse

In the process of using Django and Angular, I encountered a problem where Angular POST data to Django.

// Angular
$http({
    url: "myviews",
    method: "POST",
    data: {'text': 'hello world', 'date': '2017-01-04'}
})
# Django
def myviews(request):
    print request.POST
    print request.body

The above will print out

<QueryDict: {}>
u"{'text': 'hello world', 'date': '2017-01-04'}"

and we expect this result

<QueryDict: {u&#39;text&#39;: u&#39;hello world&#39;, u&#39;date&#39;: u&#39;2017-01-04&#39;}>
u"{'text': 'hello world', 'date': '2017-01-04'}"

This problem occurs because the default data format sent by Angular is JSON instead of urlencode, and Django’s request.POST cannot parse JSON, so the above result appears.

There are many solutions. The simplest and crudest way is to parse request.body in each view function.

def myviews(request):
    data = urlencode(json.loads(request.body))
    q_data = QueryDict(data)

We can do this type of operation Extract it and write it as Middlerware, and process it uniformly before the request request reaches the view function

class JSONMiddleware(object):
    """
    Process application/json requests data from GET and POST requests.
    """
    def process_request(self, request):
        if 'application/json' in request.META['CONTENT_TYPE']:
            data = json.loads(request.body)
            q_data = QueryDict('', mutable=True)

            for key, value in data.iteritems():
                if isinstance(value, list):
                    for x in value:
                        q_data.update({key: x})
                else:
                    q_data.update({key: value})

            if request.method == 'GET':
                request.GET = q_data

            if request.method == 'POST':
                request.POST = q_data
        return None

because some request requests are not included CONTENT-TYPEThis Header, so we need to judge, the reason why it is not simply converted into Dict but QueryDict is because it follows For the principle of consistency, we want to bind the result to request.GET or request.POST, which are both of QueryDict type. The biggest difference between QueryDict and Dict is that QueryDict stores each value in the list, and QueryDict is not Modification type. So when value is a list, we must also make a judgment, otherwise the entire list will be stored as one element in the list of QueryDict.

a = {"a": [123, 456, 444], "b": 456}
# 不做判断
data = QueryDict('', mutable=True)
for k, v in a.iteritems():
    data.update({k: v})
print data

# 做判断
data = QueryDict('' mutable=True)
for k, v in a.iteritems():
    if isinstance(v, list):
        for x in value:
            data.update({k: x})
    else:
        data.update({k: v})
print data
<QueryDict: {u&#39;a&#39;: [[123, 456, 444]], u&#39;b&#39;: [456]}>
<QueryDict: {u&#39;a&#39;: [123, 456, 444], u&#39;b&#39;: [456]}>

                                                  ​ ​ ​ ​ ​


#For more articles related to Django parsing Angular’s ​​POST data, please pay attention to the PHP Chinese website!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn