On Fri, Jul 16, 2010 at 6:27 PM, Joel Klabo <[email protected]> wrote:

> I am using modelForm to make a form for my Brew model. The form shows
> up and works fine. And saves it when I call save. But it does not
> catch the errors correctly. I set it up mostly based on this example:
>
> from django.core.validators import ValidationError, NON_FIELD_ERRORS
> try:
>    article.full_clean()
> except ValidationError, e:
>    non_field_errors = e.message_dict[NON_FIELD_ERRORS]
>
>
> First of all, NON_FIELD_ERRORS was unknown to django and caused an
> error.
>
>
That was an error in the doc. The import should be:

from django.core.exceptions import ValidationError, NON_FIELD_ERRORS



> What I really want is to see a full code example of how to do this
> seemingly simple thing. Just a form from a model that allows users to
> create a new instance of the model and save it to the database. And
> how the errors are dealt with. Please help, I swear I have looked all
> through the docs.
>

The simplest thing is the regular Django form idiom of calling is_valid() on
the form and re-displaying the form (now annotated with error information)
in the case where is_valid() returns False, see:
http://docs.djangoproject.com/en/1.2/topics/forms/#using-a-form-in-a-view

The fragment of code you posted is from the documentation for the new model
validation feature, but it is not clear from what you have written that you
actually want/need to use that feature? For the simplest case, it isn't
needed.

Karen
 --
http://tracey.org/kmt/

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to