Getting post data in django starts with specifying what type of data the post is sending.
1. Get the form key value data in POST
If you want to get the form data in django's POST method, define the request data type in the post request header before the client sends the POST data using JavaScript:
("Content-type","application/x-www-form-urlencoded");
In django's related methods, you need to get the form's key-value data, and you can do so by getting the string content of the entire form data
if( == 'POST'): print("the POST method") concat = postBody = print(concat) print(type(postBody)) print(postBody)
Related logs:
the POST method
<QueryDict: {u'username': [u'abc'], u'password': [u'123']}>
<type 'str'>
username=abc&password=123
2. Get the data in json format in POST
If you want to get data in json format in django's POST method, you need to set the request data type in the post request header:
("Content-type","application/json");
Import python's json module (import json) in django, then use the method to get the content in the form of a json string and use () to load the data.
if( == 'POST'): print("the POST method") concat = postBody = print(concat) print(type(postBody)) print(postBody) json_result = (postBody) print(json_result)
Related logs:
the POST method
<QueryDict: {}>
<type 'str'>
{"sdf":23}
{u'sdf': 23}
This is the whole content of this article.