From 066bf42675040abd7b1a42e5559890e5f9881058 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Mon, 25 Mar 2013 21:25:53 +0100 Subject: [PATCH] Removed forced typecasting of help_text/label Field arguments In any case, setting those variables to non-ascii utf-8 bytestrings is now considered a programming error. --- django/forms/fields.py | 13 ++++--------- django/forms/models.py | 4 ++-- tests/forms_tests/tests/forms.py | 29 +++++++---------------------- 3 files changed, 13 insertions(+), 33 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index ecad857f72..0a0aa12f98 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -61,7 +61,7 @@ class Field(object): creation_counter = 0 def __init__(self, required=True, widget=None, label=None, initial=None, - help_text=None, error_messages=None, show_hidden_initial=False, + help_text='', error_messages=None, show_hidden_initial=False, validators=[], localize=False): # required -- Boolean that specifies whether the field is required. # True by default. @@ -82,14 +82,9 @@ class Field(object): # hidden widget with initial value after widget. # validators -- List of addtional validators to use # localize -- Boolean that specifies if the field should be localized. - if label is not None: - label = smart_text(label) self.required, self.label, self.initial = required, label, initial self.show_hidden_initial = show_hidden_initial - if help_text is None: - self.help_text = '' - else: - self.help_text = smart_text(help_text) + self.help_text = help_text widget = widget or self.widget if isinstance(widget, type): widget = widget() @@ -739,7 +734,7 @@ class ChoiceField(Field): } def __init__(self, choices=(), required=True, widget=None, label=None, - initial=None, help_text=None, *args, **kwargs): + initial=None, help_text='', *args, **kwargs): super(ChoiceField, self).__init__(required=required, widget=widget, label=label, initial=initial, help_text=help_text, *args, **kwargs) self.choices = choices @@ -999,7 +994,7 @@ class MultiValueField(Field): class FilePathField(ChoiceField): def __init__(self, path, match=None, recursive=False, allow_files=True, allow_folders=False, required=True, widget=None, label=None, - initial=None, help_text=None, *args, **kwargs): + initial=None, help_text='', *args, **kwargs): self.path, self.match, self.recursive = path, match, recursive self.allow_files, self.allow_folders = allow_files, allow_folders super(FilePathField, self).__init__(choices=(), required=required, diff --git a/django/forms/models.py b/django/forms/models.py index 39d753b1a6..5185e17c32 100644 --- a/django/forms/models.py +++ b/django/forms/models.py @@ -935,7 +935,7 @@ class ModelChoiceField(ChoiceField): def __init__(self, queryset, empty_label="---------", cache_choices=False, required=True, widget=None, label=None, initial=None, - help_text=None, to_field_name=None, *args, **kwargs): + help_text='', to_field_name=None, *args, **kwargs): if required and (initial is not None): self.empty_label = None else: @@ -1031,7 +1031,7 @@ class ModelMultipleChoiceField(ModelChoiceField): def __init__(self, queryset, cache_choices=False, required=True, widget=None, label=None, initial=None, - help_text=None, *args, **kwargs): + help_text='', *args, **kwargs): super(ModelMultipleChoiceField, self).__init__(queryset, None, cache_choices, required, widget, label, initial, help_text, *args, **kwargs) diff --git a/tests/forms_tests/tests/forms.py b/tests/forms_tests/tests/forms.py index ff4630b7d6..310525e0a5 100644 --- a/tests/forms_tests/tests/forms.py +++ b/tests/forms_tests/tests/forms.py @@ -952,12 +952,12 @@ class FormsTestCase(TestCase): class UserRegistration(Form): username = CharField(max_length=10, label='Your username') password1 = CharField(widget=PasswordInput) - password2 = CharField(widget=PasswordInput, label='Password (again)') + password2 = CharField(widget=PasswordInput, label='Contraseña (de nuevo)') p = UserRegistration(auto_id=False) self.assertHTMLEqual(p.as_ul(), """
  • Your username:
  • Password1:
  • -
  • Password (again):
  • """) +
  • Contraseña (de nuevo):
  • """) # Labels for as_* methods will only end in a colon if they don't end in other # punctuation already. @@ -979,14 +979,6 @@ class FormsTestCase(TestCase):

    """) - # A label can be a Unicode object or a bytestring with special characters. - class UserRegistration(Form): - username = CharField(max_length=10, label='ŠĐĆŽćžšđ') - password = CharField(widget=PasswordInput, label='\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111') - - p = UserRegistration(auto_id=False) - self.assertHTMLEqual(p.as_ul(), '
  • \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111:
  • \n
  • \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111:
  • ') - # If a label is set to the empty string for a field, that field won't get a label. class UserRegistration(Form): username = CharField(max_length=10, label='') @@ -1246,20 +1238,20 @@ class FormsTestCase(TestCase): # You can specify descriptive text for a field by using the 'help_text' argument) class UserRegistration(Form): username = CharField(max_length=10, help_text='e.g., user@example.com') - password = CharField(widget=PasswordInput, help_text='Choose wisely.') + password = CharField(widget=PasswordInput, help_text='Wählen Sie mit Bedacht.') p = UserRegistration(auto_id=False) self.assertHTMLEqual(p.as_ul(), """
  • Username: e.g., user@example.com
  • -
  • Password: Choose wisely.
  • """) +
  • Password: Wählen Sie mit Bedacht.
  • """) self.assertHTMLEqual(p.as_p(), """

    Username: e.g., user@example.com

    -

    Password: Choose wisely.

    """) +

    Password: Wählen Sie mit Bedacht.

    """) self.assertHTMLEqual(p.as_table(), """Username:
    e.g., user@example.com -Password:
    Choose wisely.""") +Password:
    Wählen Sie mit Bedacht.""") # The help text is displayed whether or not data is provided for the form. p = UserRegistration({'username': 'foo'}, auto_id=False) self.assertHTMLEqual(p.as_ul(), """
  • Username: e.g., user@example.com
  • -
  • Password: Choose wisely.
  • """) +
  • Password: Wählen Sie mit Bedacht.
  • """) # help_text is not displayed for hidden fields. It can be used for documentation # purposes, though. @@ -1272,13 +1264,6 @@ class FormsTestCase(TestCase): self.assertHTMLEqual(p.as_ul(), """
  • Username: e.g., user@example.com
  • Password:
  • """) - # Help text can include arbitrary Unicode characters. - class UserRegistration(Form): - username = CharField(max_length=10, help_text='ŠĐĆŽćžšđ') - - p = UserRegistration(auto_id=False) - self.assertHTMLEqual(p.as_ul(), '
  • Username: \u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111
  • ') - def test_subclassing_forms(self): # You can subclass a Form to add fields. The resulting form subclass will have # all of the fields of the parent Form, plus whichever fields you define in the