On Tue, Mar 15, 2011 at 2:14 PM, Ben Dembroski <i...@ionraystudios.com> wrote:
> Hi all,
>
> I'm trying to pass a ValidationError as a string to a template, but I
> can't seem to determine where to find the 'special key' that is
> referred to in the documentation here:
>
> http://docs.djangoproject.com/en/dev/ref/models/instances/
>
> {quote}
> Any ValidationError raised by Model.clean() will be stored under a
> special key that is used for errors that are tied to the entire model
> instead of to a specific field. You can access these errors with
> NON_FIELD_ERRORS:
> {endquote}
>
> Here's my snippet of code:
>
>                        try:
>                                peep.save()
>                        except ValidationError, e:
>                                errormsg = e.invalid[NON_FIELD_ERRORS]
>                                form = EditPerson()
>                                return render_to_response('personentry.html', 
> { 'form' : form,
> 'errormsg': errormsg  },context_instance=RequestContext(request))
>
> The error is being thrown when when making an invalid entry into a
> DateField.  Using the code above, if I make:
> erromsg = e
>
> I get:
>
> [u'Invalid date: month must be in 1..12']
> or
> [u'Enter a valid date in YYYY-MM-DD format.']
>
> Are two examples, depending on what is passed to the model on
> peep.save().
> How can I find out what the 'special key' is ?
>
> Any ideas?
>

I'm confused about what you are actually asking. What doesn't work?
What error occurs? You say you can't determine the 'special key' - do
you mean NON_FIELD_ERRORS? The example clearly shows where to import
this constant from.

Your code is different from the example in the manual. You are
accessing e.invalid, where as the docs tell you to access
e.message_dict.

If you correct your code to fix these two points, and retry your code,
what happens? Details, not "it still doesn't work", please.

Cheers

Tom

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

Reply via email to