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'text': u'hello world', u'date': u'2017-01-04'}> 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-TYPE
This 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'a': [[123, 456, 444]], u'b': [456]}> <QueryDict: {u'a': [123, 456, 444], u'b': [456]}>
#For more articles related to Django parsing Angular’s POST data, please pay attention to the PHP Chinese website!