On Mar 15, 2:22 pm, Tom Evans <tevans...@googlemail.com> wrote: > 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
Hi Tom, Thanks for your reply. (You're right, my example was lousy). I did in fact try what you suggested earlier on, and got the the following error: AttributeError at /people/add/ 'ValidationError' object has no attribute 'message_dict' Request Method: POST Request URL: http://localhost:8000/people/add/ Django Version: 1.3 rc 1 SVN-15755 Exception Type: AttributeError Exception Value: 'ValidationError' object has no attribute 'message_dict' Exception Location: /home/benjamin/Ion/ellieharrison/trajectories/ views.py in playeradd, line 298 Python Executable: /usr/bin/python Python Version: 2.6.6 Python Path: ['/home/benjamin/Ion/ellieharrison', '/usr/lib/python2.6', '/usr/lib/python2.6/plat-linux2', '/usr/lib/python2.6/lib-tk', '/usr/lib/python2.6/lib-old', '/usr/lib/python2.6/lib-dynload', '/usr/lib/python2.6/dist-packages', '/usr/lib/python2.6/dist-packages/PIL', '/home/benjamin/django-trunk', '/usr/lib/python2.6/dist-packages/gst-0.10', '/usr/lib/pymodules/python2.6', '/usr/lib/python2.6/dist-packages/gtk-2.0', '/usr/lib/pymodules/python2.6/gtk-2.0'] Server time: Tue, 15 Mar 2011 14:27:13 +0000 I have this in the code: from django.core.exceptions import ValidationError, NON_FIELD_ERRORS and the code is currently so: except ValidationError, e: errormsg = e.message_dict[NON_FIELD_ERRORS] form = EditPerson() return render_to_response('personentry.html', { 'form' : form, 'errormsg': errormsg },context_instance=RequestContext(request)) Here's the full traceback: Environment: Request Method: POST Request URL: http://localhost:8000/people/add/ Django Version: 1.3 rc 1 SVN-15755 Python Version: 2.6.6 Installed Applications: ['django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.sites', 'django.contrib.messages', 'django.contrib.admin', 'trajectories'] Installed Middleware: ('django.middleware.common.CommonMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.messages.middleware.MessageMiddleware') Traceback: File "/home/benjamin/django-trunk/django/core/handlers/base.py" in get_response 111. response = callback(request, *callback_args, **callback_kwargs) File "/home/benjamin/django-trunk/django/contrib/auth/decorators.py" in _wrapped_view 23. return view_func(request, *args, **kwargs) File "/home/benjamin/Ion/ellieharrison/trajectories/views.py" in playeradd 298. errormsg = e.message_dict[NON_FIELD_ERRORS] Exception Type: AttributeError at /people/add/ Exception Value: 'ValidationError' object has no attribute 'message_dict' -- 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.