diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 83dc5fec20..2513380286 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -24,6 +24,11 @@ from django.utils.datastructures import MultiValueDict from django.utils.safestring import mark_safe +class FrameworkForm(Form): + name = CharField() + language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=RadioSelect) + + class Person(Form): first_name = CharField() last_name = CharField() @@ -36,6 +41,14 @@ class PersonNew(Form): birthday = DateField() +class SongForm(Form): + name = CharField() + composers = MultipleChoiceField( + choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], + widget=CheckboxSelectMultiple, + ) + + class MultiValueDictLike(dict): def getlist(self, key): return [self[key]] @@ -580,10 +593,6 @@ class FormsTestCase(SimpleTestCase): def test_forms_with_radio(self): # Add widget=RadioSelect to use that widget with a ChoiceField. - class FrameworkForm(Form): - name = CharField() - language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=RadioSelect) - f = FrameworkForm(auto_id=False) self.assertHTMLEqual(str(f['language']), """
@@ -649,16 +658,6 @@ Java

""" ) - # Test iterating on individual radios in a template - t = Template('{% for radio in form.language %}
{{ radio }}
{% endfor %}') - self.assertHTMLEqual( - t.render(Context({'form': f})), - """
-
""" - ) - def test_form_with_iterable_boundfield(self): class BeatleForm(Form): name = ChoiceField( @@ -862,13 +861,6 @@ Java def test_multiple_choice_checkbox(self): # MultipleChoiceField can also be used with the CheckboxSelectMultiple widget. - class SongForm(Form): - name = CharField() - composers = MultipleChoiceField( - choices=[('J', 'John Lennon'), ('P', 'Paul McCartney')], - widget=CheckboxSelectMultiple, - ) - f = SongForm(auto_id=False) self.assertHTMLEqual(str(f['composers']), """
@@ -884,12 +876,6 @@ Java
""") - # Test iterating on individual checkboxes in a template - t = Template('{% for checkbox in form.composers %}
{{ checkbox }}
{% endfor %}') - self.assertHTMLEqual(t.render(Context({'form': f})), """
-
""") def test_checkbox_auto_id(self): # Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox @@ -2582,213 +2568,6 @@ Password: 'File1:', ) - def test_basic_processing_in_view(self): - class UserRegistration(Form): - username = CharField(max_length=10) - password1 = CharField(widget=PasswordInput) - password2 = CharField(widget=PasswordInput) - - def clean(self): - if (self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and - self.cleaned_data['password1'] != self.cleaned_data['password2']): - raise ValidationError('Please make sure your passwords match.') - - return self.cleaned_data - - def my_function(method, post_data): - if method == 'POST': - form = UserRegistration(post_data, auto_id=False) - else: - form = UserRegistration(auto_id=False) - - if form.is_valid(): - return 'VALID: %r' % sorted(form.cleaned_data.items()) - - t = Template( - '
\n' - '\n{{ form }}\n
\n\n
' - ) - return t.render(Context({'form': form})) - - # Case 1: GET (an empty form, with no errors).) - self.assertHTMLEqual(my_function('GET', {}), """
- - - - -
Username:
Password1:
Password2:
- -
""") - # Case 2: POST with erroneous data (a redisplayed form, with errors).) - self.assertHTMLEqual( - my_function('POST', {'username': 'this-is-a-long-username', 'password1': 'foo', 'password2': 'bar'}), - """
- - - - - -
  • Please make sure your passwords match.
Username:
    -
  • Ensure this value has at most 10 characters (it has 23).
-
Password1:
Password2:
- -
""" - ) - # Case 3: POST with valid data (the success message).) - self.assertEqual( - my_function('POST', {'username': 'adrian', 'password1': 'secret', 'password2': 'secret'}), - "VALID: [('password1', 'secret'), ('password2', 'secret'), ('username', 'adrian')]" - ) - - def test_templates_with_forms(self): - class UserRegistration(Form): - username = CharField(max_length=10, help_text="Good luck picking a username that doesn't already exist.") - password1 = CharField(widget=PasswordInput) - password2 = CharField(widget=PasswordInput) - - def clean(self): - if (self.cleaned_data.get('password1') and self.cleaned_data.get('password2') and - self.cleaned_data['password1'] != self.cleaned_data['password2']): - raise ValidationError('Please make sure your passwords match.') - - return self.cleaned_data - - # You have full flexibility in displaying form fields in a template. Just pass a - # Form instance to the template, and use "dot" access to refer to individual - # fields. Note, however, that this flexibility comes with the responsibility of - # displaying all the errors, including any that might not be associated with a - # particular field. - t = Template('''
-{{ form.username.errors.as_ul }}

-{{ form.password1.errors.as_ul }}

-{{ form.password2.errors.as_ul }}

- -
''') - self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """
-

-

-

- -
""") - self.assertHTMLEqual( - t.render(Context({'form': UserRegistration({'username': 'django'}, auto_id=False)})), - """
-

-

-

- -

- -
""" - ) - - # Use form.[field].label to output a field's label. You can specify the label for - # a field by using the 'label' argument to a Field class. If you don't specify - # 'label', Django will use the field name with underscores converted to spaces, - # and the initial letter capitalized. - t = Template('''
-

-

-

- -
''') - self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """
-

-

-

- -
""") - - # User form.[field].label_tag to output a field's label with a