Tuesday, November 5, 2013

Re: DecimalField

On Tue, Aug 27, 2013 at 2:43 PM, Derrick Jackson
<derrick.jackson73@gmail.com> wrote:
> Thanks Tom. I actually tried that at one point and this is the error I
> received: not all arguments converted during string formatting.

To fix your problem, ensure that your error messages have the same
format specifiers as the defaults, which are here:

'invalid': _('Enter a number.'),
'max_value': _('Ensure this value is less than or equal to
%(limit_value)s.'),
'min_value': _('Ensure this value is greater than or equal to
%(limit_value)s.'),
'max_digits': _('Ensure that there are no more than %s digits
in total.'),
'max_decimal_places': _('Ensure that there are no more than %s
decimal places.'),
'max_whole_digits': _('Ensure that there are no more than %s
digits before the decimal point.')


The problem arises because the documentation seems incorrect for 1.5
(and trunk):

"""
The max_value and min_value error messages may contain
%(limit_value)s, which will be substituted by the appropriate limit.
"""

https://docs.djangoproject.com/en/1.5/ref/forms/fields/#decimalfield

where as the code indicates that min_value, max_value, max_digits,
max_decimal_places and max_whole_digits all take a format argument:

https://github.com/django/django/blob/stable/1.5.x/django/forms/fields.py#L260

Furthermore, the docs say "may contain", which is very misleading, as
the OP has shown, the qualifier has to be "MUST contain" - there is
nothing optional about it:

> $ python
Python 2.7.5 (default, Sep 13 2013, 10:44:18)
[GCC 4.2.1 20070831 patched [FreeBSD]] on freebsd9
Type "help", "copyright", "credits" or "license" for more information.
>>> from django import forms
>>> class BadForm(forms.Form):
... dec = forms.DecimalField(max_digits=3,
error_messages={'max_digits':'too many digits'})
...
>>> b=BadForm({'dec': '12.3456789'})
>>> b.is_valid()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 126, in is_valid
return self.is_bound and not bool(self.errors)
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 117, in _get_errors
self.full_clean()
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 272, in full_clean
self._clean_fields()
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/forms.py",
line 287, in _clean_fields
value = field.clean(value)
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/fields.py",
line 155, in clean
self.validate(value)
File "/usr/home/tom/tmp/djforms15/env/lib/python2.7/site-packages/django/forms/fields.py",
line 320, in validate
raise ValidationError(self.error_messages['max_digits'] % self.max_digits)
TypeError: not all arguments converted during string formatting



Cheers

Tom

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAFHbX1J2szHu6YTwpo%3DaOY2uN2XaQ08sqC6CvvJr9oO6LwTzEg%40mail.gmail.com.
For more options, visit https://groups.google.com/groups/opt_out.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate