[Answer]-Django AngularJS JSONResponse view in rendering json output

1👍

JSONResponse is probably using standard python json encoder, which doesn’t really encode the object for you, instead it outputs a string representation (repr) of it, hence the <Category: The New Category> output.

You might need to use some external serializer class to handle django objects, like:

http://web.archive.org/web/20120414135953/http://www.traddicts.org/webdevelopment/flexible-and-simple-json-serialization-for-django

If not, you should then normalize object into simple python types inside the view (dict, list, string.., the kind that json module has no problem encoding). So instead doing:

'category':r.category

you could do:

'category': {'name': r.category.name}

Also as a sidenote: using exec is super bad idea. Don’t use it in production!

0👍

You should return json strings for Angular:

import json
def resource_view(request):
    # something to do here
    return HttpResponse(json.dumps(your_dictionary))

for better usage i recommend djangorestframework.

Off Topic:

$http.get("{% url 'search-requests-show' %}?term="+$scope.term);

you can pass ‘param’ object:

$http.get("{% url 'search-requests-show' %}", {param : {term:$scope.term}});
👤McAbra

Leave a comment