form.errors.as_json() returns a str - Django 1.8

119 views
Skip to first unread message

George L.

unread,
Apr 21, 2015, 1:00:37 PM4/21/15
to django...@googlegroups.com
I'm trying to return errors to an ajax call but as_json() form method generates a string of a dict instead of a dict. JsonResponse accepts only dicts and if I set safe=False, JsonResponse tries to serialize the data as a string, which results in a corrupted data. 

Is this a bug or expected result?

Tom Evans

unread,
Apr 22, 2015, 11:59:25 AM4/22/15
to django...@googlegroups.com
Expected result. JSON is text, if it returned a dictionary, as_json()
wouldn't be a good name!

JsonResponse is a util for turning dictionaries into JSON, if you
actually have JSON strings, you could do something like this:

json_rsp = HttpResponse(frm.as_json(), content_type='application/json')

Cheers

Tom
Reply all
Reply to author
Forward
0 new messages