Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[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 asendecka authored timgraham committed
Showing with 36 additions and 0 deletions.
  1. +3 −0  django/forms/fields.py
  2. +33 −0 tests/i18n/tests.py
View
3  django/forms/fields.py
@@ -118,6 +118,8 @@ def __init__(self, required=True, widget=None, label=None, initial=None,
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
View
33 tests/i18n/tests.py
@@ -774,6 +774,39 @@ def test_localize_templatetag_and_filter(self):
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),
+ '<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" />'
+ )
+ self.assertHTMLEqual(
+ template_as_text.render(context),
+ '<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" />'
+ )
+ self.assertHTMLEqual(
+ template_as_hidden.render(context),
+ '<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" />'
+ )
+
+
class MiscTests(TransRealMixin, TestCase):
def setUp(self):
Please sign in to comment.
Something went wrong with that request. Please try again.