Ticket #21752: 21752-1.diff

File 21752-1.diff, 2.1 KB (added by Claude Paroz, 11 years ago)
  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 3176ce6..1002642 100644
    a b class FloatField(IntegerField):  
    289289
    290290    def widget_attrs(self, widget):
    291291        attrs = super(FloatField, self).widget_attrs(widget)
    292         if isinstance(widget, NumberInput):
     292        if isinstance(widget, NumberInput) and 'step' not in widget.attrs:
    293293            attrs.setdefault('step', 'any')
    294294        return attrs
    295295
    class DecimalField(IntegerField):  
    377377
    378378    def widget_attrs(self, widget):
    379379        attrs = super(DecimalField, self).widget_attrs(widget)
    380         if isinstance(widget, NumberInput):
     380        if isinstance(widget, NumberInput) and 'step' not in widget.attrs:
    381381            if self.decimal_places is not None:
    382382                # Use exponential notation for small values since they might
    383383                # be parsed as 0 otherwise. ref #20765
  • tests/forms_tests/tests/test_fields.py

    diff --git a/tests/forms_tests/tests/test_fields.py b/tests/forms_tests/tests/test_fields.py
    index fe11370..78b74ae 100644
    a b class FieldsTests(SimpleTestCase):  
    282282        self.assertEqual(f.max_value, 1.5)
    283283        self.assertEqual(f.min_value, 0.5)
    284284
     285    def test_floatfield_widget_attrs(self):
     286        f = FloatField(widget=NumberInput(attrs={'step': 0.01, 'max': 1.0, 'min': 0.0}))
     287        self.assertWidgetRendersTo(f, '<input step="0.01" name="f" min="0.0" max="1.0" type="number" id="id_f" />')
     288
    285289    def test_floatfield_localized(self):
    286290        """
    287291        Make sure localized FloatField's widget renders to a text input with
    class FieldsTests(SimpleTestCase):  
    392396        self.assertEqual(f.widget_attrs(NumberInput()), {'step': '1e-19'})
    393397        f = DecimalField(max_digits=20)
    394398        self.assertEqual(f.widget_attrs(NumberInput()), {'step': 'any'})
     399        f = DecimalField(max_digits=6, widget=NumberInput(attrs={'step': '0.01'}))
     400        self.assertWidgetRendersTo(f, '<input step="0.01" name="f" type="number" id="id_f" />')
    395401
    396402    def test_decimalfield_localized(self):
    397403        """
Back to Top