Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #18777 -- Localized form fields with as_text/as_hidden

Thanks croldan for the report.

Backport of 893d8de from master
  • Loading branch information...
commit 6e4fd816c48b6ffd06fb7ae2c8ef2009bb0c6c25 1 parent 196cc87
asendecka authored February 24, 2013 timgraham committed August 02, 2013
3  django/forms/fields.py
@@ -118,6 +118,8 @@ def __init__(self, required=True, widget=None, label=None, initial=None,
118 118
         super(Field, self).__init__()
119 119
 
120 120
     def prepare_value(self, value):
  121
+        if self.widget.is_localized:
  122
+            value = formats.localize_input(value)
121 123
         return value
122 124
 
123 125
     def to_python(self, value):
@@ -460,6 +462,7 @@ class DateTimeField(BaseTemporalField):
460 462
     }
461 463
 
462 464
     def prepare_value(self, value):
  465
+        value = super(DateTimeField, self).prepare_value(value)
463 466
         if isinstance(value, datetime.datetime):
464 467
             value = to_current_timezone(value)
465 468
         return value
33  tests/i18n/tests.py
@@ -774,6 +774,39 @@ def test_localize_templatetag_and_filter(self):
774 774
                 self.assertEqual(template2.render(context), output2)
775 775
                 self.assertEqual(template3.render(context), output3)
776 776
 
  777
+    def test_localized_as_text_as_hidden_input(self):
  778
+        """
  779
+        Tests if form input with 'as_hidden' or 'as_text' is correctly localized. Ticket #18777
  780
+        """
  781
+        self.maxDiff = 1200
  782
+
  783
+        with translation.override('de-at', deactivate=True):
  784
+            template = Template('{% load l10n %}{{ form.date_added }}; {{ form.cents_paid }}')
  785
+            template_as_text = Template('{% load l10n %}{{ form.date_added.as_text }}; {{ form.cents_paid.as_text }}')
  786
+            template_as_hidden = Template('{% load l10n %}{{ form.date_added.as_hidden }}; {{ form.cents_paid.as_hidden }}')
  787
+            form = CompanyForm({
  788
+                'name': 'acme',
  789
+                'date_added': datetime.datetime(2009, 12, 31, 6, 0, 0),
  790
+                'cents_paid': decimal.Decimal('59.47'),
  791
+                'products_delivered': 12000,
  792
+                })
  793
+            context = Context({'form': form })
  794
+            self.assertTrue(form.is_valid())
  795
+
  796
+            self.assertHTMLEqual(
  797
+                template.render(context),
  798
+                '<input id="id_date_added" name="date_added" type="text" value="31.12.2009 06:00:00" />; <input id="id_cents_paid" name="cents_paid" type="text" value="59,47" />'
  799
+            )
  800
+            self.assertHTMLEqual(
  801
+                template_as_text.render(context),
  802
+                '<input id="id_date_added" name="date_added" type="text" value="31.12.2009 06:00:00" />; <input id="id_cents_paid" name="cents_paid" type="text" value="59,47" />'
  803
+            )
  804
+            self.assertHTMLEqual(
  805
+                template_as_hidden.render(context),
  806
+                '<input id="id_date_added" name="date_added" type="hidden" value="31.12.2009 06:00:00" />; <input id="id_cents_paid" name="cents_paid" type="hidden" value="59,47" />'
  807
+            )
  808
+
  809
+
777 810
 class MiscTests(TransRealMixin, TestCase):
778 811
 
779 812
     def setUp(self):

0 notes on commit 6e4fd81

Please sign in to comment.
Something went wrong with that request. Please try again.