2

Suppose I am building a registration/signup or any form in general for my web app in Django and I want the same form for Web/Android/IOS. So I want the frontend and backend(Django) to communicate via JSON. My question is how can I send validation errors in JSON format if the user on any platform(Web/Android/IOS) gives me invalid data like "the email already exists" type of errors?

I need the best way possible to handle this type of scenarios and well-detailed example/explanation will be much appreciated.

Thank You.

1 Answer 1

3

The form's errors attribute has an as_json method you can use.

if form.is_valid():
    # process valid form
else:
    errors_json = form.errors.as_json()
    ...

Note the warning in the docs about escaping the errors to avoid cross site scripting attacks.

Sign up to request clarification or add additional context in comments.

4 Comments

How will the Android/IOS catch these error messages? The errors are just stored in the variable errors_json.
But I think the response consists of only those files/urls which are present inside the django's project folder.
can you explain how to loop through those errors in the js after getting them from the view with ajax
ok i posted a new question here is the link stackoverflow.com/questions/64648198/… i hope you help me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.