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");
},
});
- [Answered ]-Django assertionerror: assert token.contents == 'endif' in template?
- [Answered ]-Page not found 404 with Django
- [Answered ]-Django, ManyToManyField – ProgrammingError: relation foo_bar does not exist. Recognized in migrations, though relation is never created
Source:stackexchange.com