I ran into this same problem but the code snippets you guys gave weren't working. In case anyone is reading this in the future, here is what you want:
dict([(k, [unicode(e) for e in v]) for k,v in errors.items()]) The above line will give a dictionary of field names mapping to lists of errors. HTH On Dec 17 2008, 1:42 am, Rodrigue <rodriguealca...@gmail.com> wrote: > Hi Russell, > > I bumped into the same issue today and was glad I found this post. > However, I found that I had to use unicode() rather than str(), > which turns your example into: > > content = dict((key, [unicode(v) for v in values]) \ > for key, values in > form.errors.items()) > > With str() the proxy returns '<django.utils.functional.__proxy__ > object at 0x83878ac>' (i.e. it only puts the name into quotes) > > On Oct 19, 6:14 am, "Russell Keith-Magee" <freakboy3...@gmail.com> > wrote: > > > On Sun, Oct 19, 2008 at 12:58 AM, justind <justin.don...@gmail.com> wrote: > > > > Hello, > > > > No one has any ideas? > > > Settle down, Tiger. You asked this question on a Friday night. You may > > need to wait a little more than 18 hours if you want a response. > > We're all volunteers here, and many of us have professional and family > > obligations that take priority over answering questions on a mailing > > list. > > > To answer your question - although it may not be immediately obvious > > to you _why_ this is occurring, the error message you have received > > does tell you exactly _what_ is occurring. > > > Although form.errors appears to be a dictionary containing strings > > (i.e., a dictionay of lists of strings appear when you print > > form.errors), it's actually an ErrorDict that contains ValidationError > > objects. These, in turn, are manipulated in various ways to ensure > > correct unicode output - and one of those manipulations is the use of > > proxy objects (django.utils.functional.__proxy__). If you call str() > > on a proxy object, it will evaluate and return the underlying string, > > but the object itself isn't a string. > > > SimpleJSON (and the Django copy of the SimpleJSON library) only knows > > how to serialized basic Python types, so it complains when you give it > > a proxy object. This use of proxy objects in this way was something > > introduced by the introduction of full unicode support in Django. > > Jacob's slide predate the introduction of feature, which explains why > > the example he gave doesn't work out of the box with a more recent > > Django version. > > > However, If you force the rollout of the proxy objects before calling > > dumps(), SimpleJSON will correctly encode the form errors. Something > > like: > > > >>> simplejson.dumps(dict((key,str(v) for v in values) for key,values in > > >>> form.errors.items()) > > > should do the trick. > > > Yours, > > Russ Magee %-) > > > > The code I'm actually using in my view is almost identical to the > > > validage_contact view from > > >http://toys.jacobian.org/presentations/2007/oscon/tutorial/(single > > > slide:http://toys.jacobian.org/presentations/2007/oscon/tutorial/images/dja...) > > > and I'm using the JsonResponse function from those slides as well. > > > > Has something changed since these were published? Is this a bug? > > > > On Oct 17, 4:55 pm, "justin.don...@gmail.com" > > > <justin.don...@gmail.com> wrote: > > >> Hello, > > > >> I'm having a hard time understanding why Django won't let me serialize > > >> a dictionary of form errors. Can anyone explain why Django throws an > > >> error if I try to serialize someform.errors, even if I copy it into a > > >> plain dictionary? > > > >> #!/usr/bin/env python > > >> from django.utils import simplejson > > >> from project.main.models import SampleForm > > > >> test = {} > > >> simplejson.dumps(test) # works > > > >> test = {'key': [u"value"]} > > >> simplejson.dumps(test) # works > > > >> # suppose SampleForms wants a text and url field > > >> # I just give it a text field to test > > >> form = SampleForm({"text": "sample text"}) > > >> d = {} # make a new dictionary > > >> # update d so we're working with a plain dictionary > > >> d.update(f.errors) > > >> type(d) # returns dict > > > >> # fails: <django.utils.functional.__proxy__ object at 0x00C83810> > > >> # isnotJSONserializable > > >> simplejson.dumps(d) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---