Returning pure Django form errors in JSON

前端 未结 6 2006
青春惊慌失措
青春惊慌失措 2020-12-12 16:50

I have a Django form which I\'m validating in a normal Django view. I\'m trying to figure out how to extract the pure errors (without the HTML formatting). Below is the code

6条回答
  •  离开以前
    2020-12-12 17:50

    For Django 1.7+ use Form.errors.as_json() or something like this:

    errors = {f: e.get_json_data() for f, e in form.errors.items()}
    return json_response(success=False, data=errors)
    

提交回复
热议问题