[Answer]-Sending data from Django Function to Javascript (no refreshing)

1👍

views.py

# data should be a list of dictionaries and not a queryset
return HttpResponse(json.dumps(data), content_type="application/json")

livestream/main.html

$.ajax({
  success: function(data) {
    console.log(data); // here is your server response
  }
});

0👍

Just change your view to return JSON:

import json

from django.http import HttpResponse


@csrf_exempt
def postdata(request):
    r = requests.post(url ,headers=headers, auth=auth, data=json.dumps(data))
    return HttpResponse(json.dumps(r, ensure_ascii=False),
        content_type='application/json')

Then use that JSON to do whatever you need on the client-side.

Leave a comment