views:

288

answers:

1

I am using

rf['email'].errors

As said in docs, I can use it to have array of errors.

[str(e) for e in rf['email'].errors]  #give me ["<django.utils.functional.__proxy__>"]

If repr or str - it gives ul's or string of array.

So it worked only when I used repr and eval together. But I think its stupid solution.

eval(`rf['email'].errors`)
+1  A: 

You have a couple options depending on the output you'd like.

Option one, use the unicode constructor to convert the data:

list_of_error_texts = [unicode(e) for e in rf['email'].errors]

(Django's proxy object implements a method that responds to unicode.)

Option two, get the ErrorList as text. This produces a newline separated list of error text, with each line preceded by an asterisk:

print rf['email'].errors.as_text()
* My error one
* My error two

Option three, use django's force_unicode function. This is like unicode, but has some extra safety features:

from django.utils.encoding import force_unicode
list_of_error_texts = [force_unicode(e) for e in rf['email'].errors]
Jarret Hardie
Do you have any idea why the behavior explained in the docs doesn't work and we have to do like you did?
Tiago
The docs promise that you can loop over the field errors, but the only examples in the docs (AFAICT) are in templates, not in python as the OP as written. When looping over the ErrorList that comes back from rf['email'], you get a series of ValidationError objects. Django's template infrastructure calls force_unicode() on them, which first checks if they have a '__unicode__' method. Since they do, that's invoked, rather than the str() of the OP's example. So I wouldn't say the behaviour in the docs doesn't work, just that the examples don't include the OP's scenario :-)
Jarret Hardie