from django.forms import ( CharField, ChoiceField, Form, IntegerField, RadioSelect, Select, TextInput, ) from django.test import SimpleTestCase from django.utils import translation from django.utils.translation import gettext_lazy class FormsI18nTests(SimpleTestCase): def test_lazy_labels(self): class SomeForm(Form): username = CharField(max_length=10, label=gettext_lazy('username')) f = SomeForm() self.assertHTMLEqual( f.as_p(), '
' '
' ) # Translations are done at rendering time, so multi-lingual apps can define forms) with translation.override('de'): self.assertHTMLEqual( f.as_p(), '' '
' ) with translation.override('pl'): self.assertHTMLEqual( f.as_p(), '' '
' ) def test_non_ascii_label(self): class SomeForm(Form): field_1 = CharField(max_length=10, label=gettext_lazy('field_1')) field_2 = CharField( max_length=10, label=gettext_lazy('field_2'), widget=TextInput(attrs={'id': 'field_2_id'}), ) f = SomeForm() self.assertHTMLEqual(f['field_1'].label_tag(), '') self.assertHTMLEqual(f['field_2'].label_tag(), '') def test_non_ascii_choices(self): class SomeForm(Form): somechoice = ChoiceField( choices=(('\xc5', 'En tied\xe4'), ('\xf8', 'Mies'), ('\xdf', 'Nainen')), widget=RadioSelect(), label='\xc5\xf8\xdf', ) f = SomeForm() self.assertHTMLEqual( f.as_p(), '' '
' '