[Django]-Using and further parsing of form.errors.as_json to return http response in Django

4πŸ‘

βœ…

The problem is that you are You are converting to JSON twice – once when you call as_json, then again when you use JsonResponse.

You could use HttpResponse with form.errors.as_json():

return HttpResponse(user_form.errors.as_json(), status = 400, content_type='application/json')

Note the warnings in the as_json docs about escaping results to avoid a cross site scripting attack. You should ensure the results are escaped if you use JsonResponse as well.

πŸ‘€Alasdair

Leave a comment