Z
Z
zigen2015-05-19 13:29:08
JavaScript
zigen, 2015-05-19 13:29:08

How to properly serialize in Django or process in JS?

Good day.
I'm trying to serialize form.error.items() into a view and process this js:

if user_form.is_valid():
\\....
else:
    data = dict([(k, [e for e in v]) for k,v in user_form.errors.items()])
    return JsonResponse(data)

When I process on the client side, I get a problem, because json is not valid:
{"username": ["A user with that username already exists."]}
Because []
success : function(json) {
            if (json.redirect){
            window.location.href = json.redirect;
            }
            $('.form-control').val('');
            var error = jQuery.parseJSON(data);
            $("#error").html(error.username);
            },

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
marazmiki, 2015-05-19
@zigen

This is perfectly valid JSON. It's just that djanga is designed in such a way that it returns a list of all error messages that occurred during field validation.
The easiest way to put up and write
$("#error").html(error.username[0]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question