Ionic(angular) To Django

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.

  1. Bind the URL you want to a view as usual.

  2. Add from django.views.decorators.csrf import csrf_exempt to the top of views.py.

  3. import json

  4. 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

  5. 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)

  6. Decode the json data to a dict. For example,
    requestData = json.loads(requestJsonData)

  7. Just use the dict. For example,
    requestID = requestData[‘id’]

That’s all ! Thx 4 ur reading

Hey thanks for sharing this! Pretty simple setup :smile:

Cool!!
I was wasting my time figuring out why django-cors-headers returns 404!
This way there is no need for that package! :slight_smile:
Your precious post was one of those time savings :wink: !
Thanks.