[Answered ]-Send Model Data through AJAX in Django

1👍

I have had similar issues, and instead of django’s serializers I switched to using json.dumps and now everything works fine.

view.py

import json
def country(request):
    country = NewTable.objects.filter(id=1).values()
    return HttpResponse(json.dumps(country),content_type='application/json')

EDIT: I am not sure you can use get() for values(), i am using filter in my code and that might be why yours isnt working

1👍

I’ve found the solution

view.py

from django.core import serializers
import json
    def country(request):
    obj_json = serializers.serialize('json', NewTable.objects.filter(id=1) )
    obj_list = json.loads( obj_json )
    json_data = json.dumps( obj_list )
    return HttpResponse( json_data, mimetype='application/json' )

template

$.ajax({
type: "GET",
dataType: "json",
url:"/myapp/country",
success: function(data)
{
    alert("Hello");
},

});

Leave a comment