Issue
In a view function, I have something like:
try:
url = request.POST.get('u', '')
if len(url) == 0:
raise ValidationError('Empty URL')
except ValidationError, err:
print err
The output is a string: [u'Empty URL']
When I try to pass the error message to my template (stuffed in a dict, something like { 'error_message': err.value }
), the template successfully gets the message (using {{ error_message }}
).
The problem is that I get the exact same string as above, [u'Empty URL']
, with the [u'...']
!
How do I get rid of that?
(Python 2.6.5, Django 1.2.4, Xubuntu 10.04)
Solution
ValidationError
actually holds multiple error messages.
The output of print err
is [u'Empty URL']
because that is the string returned by repr(err.messages)
(see ValidationError.__str__
source code).
If you want to print a single readable message out of a ValidationError
, you can concatenate the list of error messages, for example:
# Python 2
print '; '.join(err.messages)
# Python 3
print('; '.join(err.messages))
Answered By - scoffey
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.