From 893d8de6f5d0b6bf23e2ed072e18fe2e012395c6 Mon Sep 17 00:00:00 2001 From: Aleksandra Sendecka Date: Sun, 24 Feb 2013 14:57:29 +0100 Subject: [PATCH] Fixed #18777 -- Localized form fields with as_text/as_hidden Thanks croldan for the report. --- django/forms/fields.py | 3 +++ tests/i18n/tests.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/django/forms/fields.py b/django/forms/fields.py index de0c620ed0..b07ebe84ee 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 3d5cbc590e..05d1065a75 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):