K
K
krinart2013-04-16 09:29:09
Django
krinart, 2013-04-16 09:29:09

Error codes in django.forms?

On validation errors, django.forms return text messages describing the error. For building a REST API, this is not very convenient, since you also need to return an error code. You have to store a dictionary like this:

message_codes = {<br>
    'password': {<br>
        'default': errors.InvalidPassword,<br>
        'This field is required': errors.PasswordMissing,<br>
    },<br>
    'newPassword': {<br>
        'default': errors.InvalidPassword,<br>
        'This field is required': errors.PasswordMissing,<br>
    },<br>
}<br>

Does anyone know or have come across how this can be improved, perhaps even by third-party means? I stumbled upon this thing pypi.python.org/pypi/validate , but have not had time to dig deep yet.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
barker, 2013-04-16
@barker

Well, there’s probably no more truth here, because. only instances with text come from clean, and they are collected inside also with text into a dictionary by field names. This piece under the link as I understood, does the same (matches). In your clean (forms, fields, etc.) you can somehow return with the code and whatever you like, it's just not very clear at what level you have the REST API. If it is higher than the forms, then you won’t do it in any way, because. going inside the forms anyway in the dictionary. If it’s lower, well, then either your own validations, or + in addition, overlap the standard ones.

R
realduke, 2013-04-16
@realduke

In general, they usually try to isolate the data processing logic into separate modules. And to build a REST API, they use special tools like tastypieapi.org or django-rest-framework.org .
You need to handle GET, PUT, UPDATE, DELETE, etc. How do you do it with forms?

R
realduke, 2013-04-16
@realduke

And it's still not entirely clear: if you have a REST API, then why do you need to handle form errors, and if you just want to use AJAX and form errors serialized in JSON, then why text messages do not suit you?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question