diff --git a/django/forms/fields.py b/django/forms/fields.py index a794c02e9f..76ee9b91d3 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -118,6 +118,8 @@ class Field(object): super(Field, self).__init__() def prepare_value(self, value): + if self.widget.is_localized: + value = formats.localize_input(value) return value def to_python(self, value): @@ -460,6 +462,7 @@ class DateTimeField(BaseTemporalField): } def prepare_value(self, value): + value = super(DateTimeField, self).prepare_value(value) if isinstance(value, datetime.datetime): value = to_current_timezone(value) return value diff --git a/tests/i18n/tests.py b/tests/i18n/tests.py index 019ed88cdb..bd61943cc1 100644 --- a/tests/i18n/tests.py +++ b/tests/i18n/tests.py @@ -774,6 +774,39 @@ class FormattingTests(TransRealMixin, TestCase): self.assertEqual(template2.render(context), output2) self.assertEqual(template3.render(context), output3) + def test_localized_as_text_as_hidden_input(self): + """ + Tests if form input with 'as_hidden' or 'as_text' is correctly localized. Ticket #18777 + """ + self.maxDiff = 1200 + + with translation.override('de-at', deactivate=True): + template = Template('{% load l10n %}{{ form.date_added }}; {{ form.cents_paid }}') + template_as_text = Template('{% load l10n %}{{ form.date_added.as_text }}; {{ form.cents_paid.as_text }}') + template_as_hidden = Template('{% load l10n %}{{ form.date_added.as_hidden }}; {{ form.cents_paid.as_hidden }}') + form = CompanyForm({ + 'name': 'acme', + 'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0), + 'cents_paid': decimal.Decimal('59.47'), + 'products_delivered': 12000, + }) + context = Context({'form': form }) + self.assertTrue(form.is_valid()) + + self.assertHTMLEqual( + template.render(context), + '; ' + ) + self.assertHTMLEqual( + template_as_text.render(context), + '; ' + ) + self.assertHTMLEqual( + template_as_hidden.render(context), + '; ' + ) + + class MiscTests(TransRealMixin, TestCase): def setUp(self):