# -*- coding: utf-8 -*- # Tests to prevent against recurrences of earlier bugs. tests = r""" It should be possible to re-use attribute dictionaries (#3810) >>> from django.forms import * >>> extra_attrs = {'class': 'special'} >>> class TestForm(Form): ... f1 = CharField(max_length=10, widget=TextInput(attrs=extra_attrs)) ... f2 = CharField(widget=TextInput(attrs=extra_attrs)) >>> TestForm(auto_id=False).as_p() u'

F1:

\n

F2:

' ####################### # Tests for form i18n # ####################### There were some problems with form translations in #3600 >>> from django.utils.translation import ugettext_lazy, activate, deactivate >>> class SomeForm(Form): ... username = CharField(max_length=10, label=ugettext_lazy('Username')) >>> f = SomeForm() >>> print f.as_p()

Translations are done at rendering time, so multi-lingual apps can define forms early and still send back the right translation. >>> activate('de') >>> print f.as_p()

>>> activate('pl') >>> f.as_p() u'

' >>> deactivate() There was some problems with form translations in #5216 >>> class SomeForm(Form): ... field_1 = CharField(max_length=10, label=ugettext_lazy('field_1')) ... field_2 = CharField(max_length=10, label=ugettext_lazy('field_2'), widget=TextInput(attrs={'id': 'field_2_id'})) >>> f = SomeForm() >>> print f['field_1'].label_tag() >>> print f['field_2'].label_tag() Unicode decoding problems... >>> GENDERS = ((u'\xc5', u'En tied\xe4'), (u'\xf8', u'Mies'), (u'\xdf', u'Nainen')) >>> class SomeForm(Form): ... somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf') >>> f = SomeForm() >>> f.as_p() u'

' Testing choice validation with UTF-8 bytestrings as input (these are the Russian abbreviations "мес." and "шт.". >>> UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.')) >>> f = ChoiceField(choices=UNITS) >>> f.clean(u'\u0448\u0442.') u'\u0448\u0442.' >>> f.clean('\xd1\x88\xd1\x82.') u'\u0448\u0442.' Translated error messages used to be buggy. >>> activate('ru') >>> f = SomeForm({}) >>> f.as_p() u'\n

' >>> deactivate() Deep copying translated text shouldn't raise an error >>> from django.utils.translation import gettext_lazy >>> class CopyForm(Form): ... degree = IntegerField(widget=Select(choices=((1, gettext_lazy('test')),))) >>> f = CopyForm() ####################### # Miscellaneous Tests # ####################### There once was a problem with Form fields called "data". Let's make sure that doesn't come back. >>> class DataForm(Form): ... data = CharField(max_length=10) >>> f = DataForm({'data': 'xyzzy'}) >>> f.is_valid() True >>> f.cleaned_data {'data': u'xyzzy'} A form with *only* hidden fields that has errors is going to be very unusual. But we can try to make sure it doesn't generate invalid XHTML. In this case, the as_p() method is the tricky one, since error lists cannot be nested (validly) inside p elements. >>> class HiddenForm(Form): ... data = IntegerField(widget=HiddenInput) >>> f = HiddenForm({}) >>> f.as_p() u'\n

' >>> f.as_table() u'' ################################################### # Tests for XSS vulnerabilities in error messages # ################################################### # The forms layer doesn't escape input values directly because error messages # might be presented in non-HTML contexts. Instead, the message is just marked # for escaping by the template engine. So we'll need to construct a little # silly template to trigger the escaping. >>> from django.template import Template, Context >>> t = Template('{{ form.errors }}') >>> class SomeForm(Form): ... field = ChoiceField(choices=[('one', 'One')]) >>> f = SomeForm({'field': '