Recently, I’ve figured out how to post data to a Django server. Here’s a brief introduction. Hybrid apps can use resources from any host as long as the domain is in the white list, so I focus on the server side, Django.
-
Bind the URL you want to a view as usual.
-
Add from django.views.decorators.csrf import csrf_exempt to the top of views.py.
-
import json
-
Create your view and prefix it with @csrf_exempt to handle the csrftoken. (It’s greatly appreciated if there’s a better solution of csrftoken.)
Example:*#in views.py*
@csrf_exempt
def myView(request):
#your view code here -
The data posted to Django is stored in request.body, whose type is bytes. Decode it to str. For example,
requestJsonData = bytes.decode(request.body) -
Decode the json data to a dict. For example,
requestData = json.loads(requestJsonData) -
Just use the dict. For example,
requestID = requestData[‘id’]
That’s all ! Thx 4 ur reading