On Tue, Mar 15, 2011 at 2:36 PM, Ben Dembroski <i...@ionraystudios.com> wrote: > 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' >
Hmm, I think the docs are, if not wrong, misleading. From looking at the code (1.2/trunk), ValidationError only has an attribute message_dict if it is constructed with a dictionary (I let out an audible 'ewww' as I read the code). If it is constructed with a string or a list of strings, then it should have an attribute messages, which is a simple list of strings. There is also a method update_error_dict(). I think using this, you can write some code which works in all cases: from django.core.exceptions import ValidationError, NON_FIELD_ERRORS try: ... except ValidationError, e: message_dict = e.update_error_dict({}) print message_dict[NON_FIELD_ERRORS] Just while I'm on this class, the update_error_dict() method will happily take None as an argument if it is constructed with a dictionary (by design, it checks for it), but blows up if it is constructed with a list or a string: >>> e=ValidationError({'foo': 'bar'}) >>> e.update_error_dict(None) {'foo': 'bar'} >>> e=ValidationError('foo bar') >>> e.update_error_dict(None) Traceback (most recent call last): File "<console>", line 1, in <module> File "django/core/exceptions.py", line 85, in update_error_dict error_dict[NON_FIELD_ERRORS] = self.messages TypeError: 'NoneType' object does not support item assignment This class is horrendous - however it is constructed, it should present a consistent interface. It can't even present a consistent method. Dirty, Bad and Wrong. 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.