From ec6121693f112ae33b653b4364e812722d2eb567 Mon Sep 17 00:00:00 2001
From: Jon Dufresne
Date: Mon, 28 Mar 2016 11:02:04 -0700
Subject: Fixed #22383 -- Added support for HTML5 required attribute on
required form fields.
---
tests/forms_tests/field_tests/test_charfield.py | 2 +-
tests/forms_tests/field_tests/test_choicefield.py | 2 +-
tests/forms_tests/field_tests/test_decimalfield.py | 11 +-
.../forms_tests/field_tests/test_durationfield.py | 2 +-
tests/forms_tests/field_tests/test_emailfield.py | 7 +-
tests/forms_tests/field_tests/test_floatfield.py | 14 +-
tests/forms_tests/field_tests/test_integerfield.py | 10 +-
.../field_tests/test_multivaluefield.py | 16 +-
tests/forms_tests/field_tests/test_urlfield.py | 4 +-
tests/forms_tests/tests/test_forms.py | 909 ++++++++++++---------
tests/forms_tests/tests/test_regressions.py | 22 +-
tests/forms_tests/tests/tests.py | 26 +-
12 files changed, 585 insertions(+), 440 deletions(-)
(limited to 'tests/forms_tests')
diff --git a/tests/forms_tests/field_tests/test_charfield.py b/tests/forms_tests/field_tests/test_charfield.py
index 0927370af3..906b91bc75 100644
--- a/tests/forms_tests/field_tests/test_charfield.py
+++ b/tests/forms_tests/field_tests/test_charfield.py
@@ -119,4 +119,4 @@ class CharFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
def test_charfield_disabled(self):
f = CharField(disabled=True)
- self.assertWidgetRendersTo(f, '')
+ self.assertWidgetRendersTo(f, '')
diff --git a/tests/forms_tests/field_tests/test_choicefield.py b/tests/forms_tests/field_tests/test_choicefield.py
index 013766d50b..69b4d9e342 100644
--- a/tests/forms_tests/field_tests/test_choicefield.py
+++ b/tests/forms_tests/field_tests/test_choicefield.py
@@ -81,6 +81,6 @@ class ChoiceFieldTest(FormFieldAssertionsMixin, SimpleTestCase):
f = ChoiceField(choices=[('J', 'John'), ('P', 'Paul')], disabled=True)
self.assertWidgetRendersTo(
f,
- '
-
+
-
"""
+"""
)
def test_unbound_form(self):
@@ -181,38 +181,38 @@ class FormsTestCase(SimpleTestCase):
self.assertHTMLEqual(
str(p),
""" |
- |
+
|
- |
+
|
- |
"""
+"""
)
self.assertHTMLEqual(
p.as_table(),
""" |
- |
+
|
- |
+
|
- |
"""
+"""
)
self.assertHTMLEqual(
p.as_ul(),
"""
-
+
-
+
-"""
+"""
)
self.assertHTMLEqual(
p.as_p(),
"""
-
+
-
+
-
"""
+"""
)
def test_unicode_values(self):
@@ -225,33 +225,33 @@ class FormsTestCase(SimpleTestCase):
self.assertHTMLEqual(
p.as_table(),
' | '
- ' |
\n'
+ '\n'
'| '
' | |
\n'
+ 'id="id_last_name" required />\n'
' | '
- ' |
'
+ ''
)
self.assertHTMLEqual(
p.as_ul(),
' '
- '\n'
+ '\n'
' '
'\n'
+ 'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" required />\n'
' '
- ''
+ ''
)
self.assertHTMLEqual(
p.as_p(),
' '
- '
\n'
+ '\n'
' '
'
\n'
+ 'value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" id="id_last_name" required />\n'
' '
- '
'
+ ''
)
p = Person({'last_name': 'Lennon'})
@@ -271,9 +271,12 @@ class FormsTestCase(SimpleTestCase):
self.assertEqual(p['first_name'].errors.as_text(), '* This field is required.')
p = Person()
- self.assertHTMLEqual(str(p['first_name']), '')
- self.assertHTMLEqual(str(p['last_name']), '')
- self.assertHTMLEqual(str(p['birthday']), '')
+ self.assertHTMLEqual(
+ str(p['first_name']),
+ '',
+ )
+ self.assertHTMLEqual(str(p['last_name']), '')
+ self.assertHTMLEqual(str(p['birthday']), '')
def test_cleaned_data_only_fields(self):
# cleaned_data will always *only* contain a key for fields defined in the
@@ -333,29 +336,29 @@ class FormsTestCase(SimpleTestCase):
self.assertHTMLEqual(
p.as_table(),
""" |
- |
+
|
- |
+
|
- |
"""
+"""
)
self.assertHTMLEqual(
p.as_ul(),
"""
-
+
-
+
-"""
+"""
)
self.assertHTMLEqual(
p.as_p(),
"""
-
+
-
+
-
"""
+"""
)
def test_auto_id_true(self):
@@ -365,11 +368,11 @@ class FormsTestCase(SimpleTestCase):
self.assertHTMLEqual(
p.as_ul(),
"""
-
+
-
+
-"""
+"""
)
def test_auto_id_false(self):
@@ -378,9 +381,9 @@ class FormsTestCase(SimpleTestCase):
p = Person(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """First name:
-Last name:
-Birthday: """
+ """First name:
+Last name:
+Birthday: """
)
def test_id_on_field(self):
@@ -390,9 +393,9 @@ class FormsTestCase(SimpleTestCase):
self.assertHTMLEqual(
p.as_ul(),
"""
-
-Last name:
-Birthday: """
+
+Last name:
+Birthday: """
)
def test_auto_id_on_form_and_field(self):
@@ -402,11 +405,11 @@ class FormsTestCase(SimpleTestCase):
self.assertHTMLEqual(
p.as_ul(),
"""
-
+
-
+
-"""
+"""
)
def test_various_boolean_values(self):
@@ -415,26 +418,33 @@ class FormsTestCase(SimpleTestCase):
get_spam = BooleanField()
f = SignupForm(auto_id=False)
- self.assertHTMLEqual(str(f['email']), '')
- self.assertHTMLEqual(str(f['get_spam']), '')
+ self.assertHTMLEqual(str(f['email']), '')
+ self.assertHTMLEqual(str(f['get_spam']), '')
f = SignupForm({'email': 'test@example.com', 'get_spam': True}, auto_id=False)
- self.assertHTMLEqual(str(f['email']), '')
- self.assertHTMLEqual(str(f['get_spam']), '')
+ self.assertHTMLEqual(str(f['email']), '')
+ self.assertHTMLEqual(
+ str(f['get_spam']),
+ '',
+ )
# 'True' or 'true' should be rendered without a value attribute
f = SignupForm({'email': 'test@example.com', 'get_spam': 'True'}, auto_id=False)
- self.assertHTMLEqual(str(f['get_spam']), '')
+ self.assertHTMLEqual(
+ str(f['get_spam']),
+ '',
+ )
f = SignupForm({'email': 'test@example.com', 'get_spam': 'true'}, auto_id=False)
- self.assertHTMLEqual(str(f['get_spam']), '')
+ self.assertHTMLEqual(
+ str(f['get_spam']), '')
# A value of 'False' or 'false' should be rendered unchecked
f = SignupForm({'email': 'test@example.com', 'get_spam': 'False'}, auto_id=False)
- self.assertHTMLEqual(str(f['get_spam']), '')
+ self.assertHTMLEqual(str(f['get_spam']), '')
f = SignupForm({'email': 'test@example.com', 'get_spam': 'false'}, auto_id=False)
- self.assertHTMLEqual(str(f['get_spam']), '')
+ self.assertHTMLEqual(str(f['get_spam']), '')
# A value of '0' should be interpreted as a True value (#16820)
f = SignupForm({'email': 'test@example.com', 'get_spam': '0'})
@@ -448,13 +458,16 @@ class FormsTestCase(SimpleTestCase):
message = CharField(widget=Textarea)
f = ContactForm(auto_id=False)
- self.assertHTMLEqual(str(f['subject']), '')
- self.assertHTMLEqual(str(f['message']), '')
+ self.assertHTMLEqual(str(f['subject']), '')
+ self.assertHTMLEqual(str(f['message']), '')
# as_textarea(), as_text() and as_hidden() are shortcuts for changing the output
# widget type:
- self.assertHTMLEqual(f['subject'].as_textarea(), '')
- self.assertHTMLEqual(f['message'].as_text(), '')
+ self.assertHTMLEqual(
+ f['subject'].as_textarea(),
+ '',
+ )
+ self.assertHTMLEqual(f['message'].as_text(), '')
self.assertHTMLEqual(f['message'].as_hidden(), '')
# The 'widget' parameter to a Field can also be an instance:
@@ -463,17 +476,20 @@ class FormsTestCase(SimpleTestCase):
message = CharField(widget=Textarea(attrs={'rows': 80, 'cols': 20}))
f = ContactForm(auto_id=False)
- self.assertHTMLEqual(str(f['message']), '')
+ self.assertHTMLEqual(str(f['message']), '')
# Instance-level attrs are *not* carried over to as_textarea(), as_text() and
# as_hidden():
- self.assertHTMLEqual(f['message'].as_text(), '')
+ self.assertHTMLEqual(f['message'].as_text(), '')
f = ContactForm({'subject': 'Hello', 'message': 'I love you.'}, auto_id=False)
self.assertHTMLEqual(
f['subject'].as_textarea(),
- ''
+ ''
+ )
+ self.assertHTMLEqual(
+ f['message'].as_text(),
+ '',
)
- self.assertHTMLEqual(f['message'].as_text(), '')
self.assertHTMLEqual(f['message'].as_hidden(), '')
def test_forms_with_choices(self):
@@ -483,12 +499,12 @@ class FormsTestCase(SimpleTestCase):
language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')])
f = FrameworkForm(auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
@@ -500,7 +516,7 @@ class FormsTestCase(SimpleTestCase):
language = ChoiceField(choices=[('', '------'), ('P', 'Python'), ('J', 'Java')])
f = FrameworkForm(auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
@@ -512,12 +528,12 @@ class FormsTestCase(SimpleTestCase):
language = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')], widget=Select(attrs={'class': 'foo'}))
f = FrameworkForm(auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
@@ -533,12 +549,12 @@ class FormsTestCase(SimpleTestCase):
)
f = FrameworkForm(auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
f = FrameworkForm({'name': 'Django', 'language': 'P'}, auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
@@ -549,10 +565,10 @@ class FormsTestCase(SimpleTestCase):
language = ChoiceField()
f = FrameworkForm(auto_id=False)
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
f.fields['language'].choices = [('P', 'Python'), ('J', 'Java')]
- self.assertHTMLEqual(str(f['language']), """
+ self.assertHTMLEqual(str(f['language']), """
""")
@@ -565,18 +581,18 @@ class FormsTestCase(SimpleTestCase):
f = FrameworkForm(auto_id=False)
self.assertHTMLEqual(str(f['language']), """""")
- self.assertHTMLEqual(f.as_table(), """| Name: | |
+ self.assertHTMLEqual(f.as_table(), """| Name: | |
| Language: | |
""")
- self.assertHTMLEqual(f.as_ul(), """Name:
+ self.assertHTMLEqual(f.as_ul(), """Name:
Language: """)
# Regarding auto_id and
- Paul McCartney
+ Paul McCartney
"""
)
@@ -864,7 +888,7 @@ class FormsTestCase(SimpleTestCase):
f = SongFormHidden(MultiValueDict(dict(name=['Yesterday'], composers=['J', 'P'])), auto_id=False)
self.assertHTMLEqual(
f.as_ul(),
- """Name:
+ """Name:
"""
)
@@ -905,10 +929,10 @@ class FormsTestCase(SimpleTestCase):
f.as_table(),
"""| <em>Special</em> Field: |
- Something's wrong with 'Nothing to escape'
- |
+
| Special Field: |
- 'Nothing to escape' is a safe string
- |
"""
+"""
)
f = EscapingForm({
'special_name': "Should escape < & > and ",
@@ -920,10 +944,10 @@ class FormsTestCase(SimpleTestCase):
- Something's wrong with 'Should escape < & > and
<script>alert('xss')</script>'
+value="Should escape < & > and <script>alert('xss')</script>" required />
| Special Field: |
- 'Do not escape' is a safe string
- |
"""
+"""
)
def test_validating_multiple_fields(self):
@@ -1010,11 +1034,11 @@ value="Should escape < & > and <script>alert('xss')</
f.as_table(),
"""| Username: |
- |
+
| Password1: |
- |
+
| Password2: |
- |
"""
+"""
)
self.assertEqual(f.errors['username'], ['This field is required.'])
self.assertEqual(f.errors['password1'], ['This field is required.'])
@@ -1026,17 +1050,17 @@ value="Should escape < & > and <script>alert('xss')</
f.as_table(),
"""
- Please make sure your passwords match.
|
-| Username: | |
-| Password1: | |
-| Password2: | |
"""
+| Username: | |
+| Password1: | |
+| Password2: | |
"""
)
self.assertHTMLEqual(
f.as_ul(),
"""
- Please make sure your passwords match.
-Username:
-Password1:
-Password2: """
+Username:
+Password1:
+Password2: """
)
f = UserRegistration({'username': 'adrian', 'password1': 'foo', 'password2': 'foo'}, auto_id=False)
@@ -1160,9 +1184,9 @@ value="Should escape < & > and <script>alert('xss')</
p = Person(auto_id=False)
self.assertHTMLEqual(
p.as_table(),
- """| First name: | |
-| Last name: | |
-| Birthday: | |
"""
+ """| First name: | |
+| Last name: | |
+| Birthday: | |
"""
)
# Instances of a dynamic Form do not persist fields from one Form instance to
@@ -1178,15 +1202,15 @@ value="Should escape < & > and <script>alert('xss')</
my_form = MyForm(field_list=field_list)
self.assertHTMLEqual(
my_form.as_table(),
- """| Field1: | |
-| Field2: | |
"""
+ """| Field1: | |
+| Field2: | |
"""
)
field_list = [('field3', CharField()), ('field4', CharField())]
my_form = MyForm(field_list=field_list)
self.assertHTMLEqual(
my_form.as_table(),
- """| Field3: | |
-| Field4: | |
"""
+ """| Field3: | |
+| Field4: | |
"""
)
class MyForm(Form):
@@ -1203,19 +1227,19 @@ value="Should escape < & > and <script>alert('xss')</
my_form = MyForm(field_list=field_list)
self.assertHTMLEqual(
my_form.as_table(),
- """| Default field 1: | |
-| Default field 2: | |
-| Field1: | |
-| Field2: | |
"""
+ """| Default field 1: | |
+| Default field 2: | |
+| Field1: | |
+| Field2: | |
"""
)
field_list = [('field3', CharField()), ('field4', CharField())]
my_form = MyForm(field_list=field_list)
self.assertHTMLEqual(
my_form.as_table(),
- """| Default field 1: | |
-| Default field 2: | |
-| Field3: | |
-| Field4: | |
"""
+ """| Default field 1: | |
+| Default field 2: | |
+| Field3: | |
+| Field4: | |
"""
)
# Similarly, changes to field attributes do not persist from one Form instance
@@ -1313,20 +1337,21 @@ value="Should escape < & > and <script>alert('xss')</
p = Person(auto_id=False)
self.assertHTMLEqual(
p.as_table(),
- """| First name: | |
-| Last name: | |
-| Birthday: | |
"""
+ """| First name: | |
+| Last name: | |
+| Birthday: |
+ |
"""
)
self.assertHTMLEqual(
p.as_ul(),
- """First name:
-Last name:
-Birthday: """
+ """First name:
+Last name:
+Birthday: """
)
self.assertHTMLEqual(
- p.as_p(), """First name:
-Last name:
-Birthday:
"""
+ p.as_p(), """First name:
+Last name:
+Birthday:
"""
)
# With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
@@ -1334,31 +1359,31 @@ value="Should escape < & > and <script>alert('xss')</
self.assertHTMLEqual(
p.as_table(),
"""| First name: |
- |
+
| Last name: |
- |
+
| Birthday: |
-
+
|
"""
)
self.assertHTMLEqual(
p.as_ul(),
"""First name:
-
+
Last name:
-
+
Birthday:
-
+
"""
)
self.assertHTMLEqual(
p.as_p(),
"""First name:
-
+
Last name:
-
+
Birthday:
-
+
"""
)
@@ -1371,26 +1396,26 @@ value="Should escape < & > and <script>alert('xss')</
p.as_table(),
"""
- (Hidden field hidden_text) This field is required.
|
-| First name: | |
-| Last name: | |
-| Birthday: |
+ |
|---|
| First name: | |
+| Last name: | |
+| Birthday: |
|
"""
)
self.assertHTMLEqual(
p.as_ul(),
"""- (Hidden field hidden_text) This field is required.
-First name:
-Last name:
-Birthday:
+First name:
+Last name:
+Birthday:
"""
)
self.assertHTMLEqual(
p.as_p(),
"""- (Hidden field hidden_text) This field is required.
-First name:
-Last name:
-Birthday:
"""
+First name:
+Last name:
+Birthday:
+
"""
)
# A corner case: It's possible for a form to have only HiddenInputs.
@@ -1422,20 +1447,20 @@ value="Should escape < & > and <script>alert('xss')</
field14 = CharField()
p = TestForm(auto_id=False)
- self.assertHTMLEqual(p.as_table(), """| Field1: | |
-| Field2: | |
-| Field3: | |
-| Field4: | |
-| Field5: | |
-| Field6: | |
-| Field7: | |
-| Field8: | |
-| Field9: | |
-| Field10: | |
-| Field11: | |
-| Field12: | |
-| Field13: | |
-| Field14: | |
""")
+ self.assertHTMLEqual(p.as_table(), """| Field1: | |
+| Field2: | |
+| Field3: | |
+| Field4: | |
+| Field5: | |
+| Field6: | |
+| Field7: | |
+| Field8: | |
+| Field9: | |
+| Field10: | |
+| Field11: | |
+| Field12: | |
+| Field13: | |
+| Field14: | |
""")
def test_explicit_field_order(self):
class TestFormParent(Form):
@@ -1494,10 +1519,10 @@ value="Should escape < & > and <script>alert('xss')</
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password:
-Realname:
-Address: """
+ """Username:
+Password:
+Realname:
+Address: """
)
# If you specify a custom "attrs" that includes the "maxlength" attribute,
@@ -1510,8 +1535,8 @@ value="Should escape < & > and <script>alert('xss')</
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password: """
+ """Username:
+Password: """
)
def test_specifying_labels(self):
@@ -1526,9 +1551,9 @@ value="Should escape < & > and <script>alert('xss')</
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Your username:
-Password1:
-Contraseña (de nuevo): """
+ """Your username:
+Password1:
+Contraseña (de nuevo): """
)
# Labels for as_* methods will only end in a colon if they don't end in other
@@ -1542,19 +1567,19 @@ value="Should escape < & > and <script>alert('xss')</
self.assertHTMLEqual(
Questions(auto_id=False).as_p(),
- """The first question:
-What is your name?
-The answer to life is:
-Answer this question!
-The last question. Period.
"""
+ """The first question:
+What is your name?
+The answer to life is:
+Answer this question!
+The last question. Period.
"""
)
self.assertHTMLEqual(
Questions().as_p(),
- """The first question:
-What is your name?
-The answer to life is:
-Answer this question!
-The last question. Period.
"""
+ """The first question:
+What is your name?
+The answer to life is:
+Answer this question!
+The last question. Period.
"""
)
# If a label is set to the empty string for a field, that field won't get a label.
@@ -1563,13 +1588,14 @@ value="Should escape < & > and <script>alert('xss')</
password = CharField(widget=PasswordInput)
p = UserRegistration(auto_id=False)
- self.assertHTMLEqual(p.as_ul(), """
-Password: """)
+ self.assertHTMLEqual(p.as_ul(), """
+Password: """)
p = UserRegistration(auto_id='id_%s')
self.assertHTMLEqual(
p.as_ul(),
- """
-Password: """
+ """
+Password:
+"""
)
# If label is None, Django will auto-create the label from the field name. This
@@ -1581,15 +1607,16 @@ value="Should escape < & > and <script>alert('xss')</
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password: """
+ """Username:
+Password: """
)
p = UserRegistration(auto_id='id_%s')
self.assertHTMLEqual(
p.as_ul(),
"""Username:
-
-Password: """
+
+Password:
+"""
)
def test_label_suffix(self):
@@ -1604,26 +1631,26 @@ value="Should escape < & > and <script>alert('xss')</
answer = CharField(label='Secret answer', label_suffix=' =')
f = FavoriteForm(auto_id=False)
- self.assertHTMLEqual(f.as_ul(), """Favorite color?
-Favorite animal:
-Secret answer = """)
+ self.assertHTMLEqual(f.as_ul(), """Favorite color?
+Favorite animal:
+Secret answer = """)
f = FavoriteForm(auto_id=False, label_suffix='?')
- self.assertHTMLEqual(f.as_ul(), """Favorite color?
-Favorite animal?
-Secret answer = """)
+ self.assertHTMLEqual(f.as_ul(), """Favorite color?
+Favorite animal?
+Secret answer = """)
f = FavoriteForm(auto_id=False, label_suffix='')
- self.assertHTMLEqual(f.as_ul(), """Favorite color?
-Favorite animal
-Secret answer = """)
+ self.assertHTMLEqual(f.as_ul(), """Favorite color?
+Favorite animal
+Secret answer = """)
f = FavoriteForm(auto_id=False, label_suffix='\u2192')
self.assertHTMLEqual(
f.as_ul(),
- 'Favorite color? \n'
- 'Favorite animal\u2192 \n'
- 'Secret answer = '
+ 'Favorite color? \n'
+ 'Favorite animal\u2192 \n'
+ 'Secret answer = '
)
def test_initial_data(self):
@@ -1640,8 +1667,8 @@ value="Should escape < & > and <script>alert('xss')</
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password: """
+ """Username:
+Password: """
)
# Here, we're submitting data, so the initial value will *not* be displayed.
@@ -1649,24 +1676,24 @@ value="Should escape < & > and <script>alert('xss')</
self.assertHTMLEqual(
p.as_ul(),
"""
-Username:
+Username:
-Password: """
+Password: """
)
p = UserRegistration({'username': ''}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
"""
-Username:
+Username:
-Password: """
+Password: """
)
p = UserRegistration({'username': 'foo'}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
+ """Username:
-Password: """
+Password: """
)
# An 'initial' value is *not* used as a fallback if data is not provided. In this
@@ -1690,14 +1717,14 @@ Password: """
p = UserRegistration(initial={'username': 'django'}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password: """
+ """Username:
+Password: """
)
p = UserRegistration(initial={'username': 'stephane'}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password: """
+ """Username:
+Password: """
)
# The 'initial' parameter is meaningless if you pass data.
@@ -1705,23 +1732,23 @@ Password: """
self.assertHTMLEqual(
p.as_ul(),
"""
-Username:
+Username:
-Password: """
+Password: """
)
p = UserRegistration({'username': ''}, initial={'username': 'django'}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
"""
-Username:
+Username:
-Password: """
+Password: """
)
p = UserRegistration({'username': 'foo'}, initial={'username': 'django'}, auto_id=False)
self.assertHTMLEqual(
- p.as_ul(), """Username:
+ p.as_ul(), """Username:
-Password: """
+Password: """
)
# A dynamic 'initial' value is *not* used as a fallback if data is not provided.
@@ -1740,8 +1767,8 @@ Password: """
p = UserRegistration(initial={'username': 'babik'}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password: """
+ """Username:
+Password: """
)
def test_callable_initial_data(self):
@@ -1769,9 +1796,9 @@ Password: """
p = UserRegistration(initial={'username': initial_django, 'options': initial_options}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password:
-Options:
+ """Username:
+Password:
+Options:
@@ -1783,11 +1810,11 @@ Password: """
self.assertHTMLEqual(
p.as_ul(),
"""
-Username:
+Username:
-Password:
+Password:
-Options:
+Options:
@@ -1797,11 +1824,11 @@ Options:
self.assertHTMLEqual(
p.as_ul(),
"""
- Username:
+ Username:
-Password:
+Password:
-Options:
+Options:
@@ -1812,10 +1839,10 @@ Options:
)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
+ """Username:
-Password:
-Options:
+Password:
+Options:
@@ -1842,9 +1869,9 @@ Password:
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password:
-Options:
+ """Username:
+Password:
+Options:
@@ -1853,9 +1880,9 @@ Password:
p = UserRegistration(initial={'username': initial_stephane, 'options': initial_options}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
-Password:
-Options:
+ """Username:
+Password:
+Options:
@@ -1925,7 +1952,7 @@ Password:
"""
class CustomWidget(TextInput):
def render(self, name, value, attrs=None):
- return format_html(str(''), ' id=custom')
+ return format_html(str(''), ' id=custom')
class SampleForm(Form):
name = CharField(widget=CustomWidget)
@@ -1988,23 +2015,23 @@ Password:
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
+ """Username:
e.g., user@example.com
-Password:
+Password:
Wählen Sie mit Bedacht."""
)
self.assertHTMLEqual(
p.as_p(),
- """Username:
+ """
Username:
e.g., user@example.com
-Password:
+
Password:
Wählen Sie mit Bedacht.
"""
)
self.assertHTMLEqual(
p.as_table(),
- """| Username: |
+ """ |
|---|
| Username: |
e.g., user@example.com |
-| Password: |
+ |
|---|
| Password: |
Wählen Sie mit Bedacht. |
"""
)
@@ -2012,9 +2039,10 @@ Password:
p = UserRegistration({'username': 'foo'}, auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
+ """Username:
e.g., user@example.com
-Password:
+
+Password:
Wählen Sie mit Bedacht."""
)
@@ -2028,9 +2056,9 @@ Password:
p = UserRegistration(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """Username:
+ """Username:
e.g., user@example.com
-Password:
+Password:
"""
)
@@ -2049,17 +2077,17 @@ Password:
p = Person(auto_id=False)
self.assertHTMLEqual(
p.as_ul(),
- """First name:
-Last name:
-Birthday: """
+ """First name:
+Last name:
+Birthday: """
)
m = Musician(auto_id=False)
self.assertHTMLEqual(
m.as_ul(),
- """First name:
-Last name:
-Birthday:
-Instrument: """
+ """First name:
+Last name:
+Birthday:
+Instrument: """
)
# Yes, you can subclass multiple forms. The fields are added in the order in
@@ -2076,11 +2104,11 @@ Password:
haircut_type = CharField()
b = Beatle(auto_id=False)
- self.assertHTMLEqual(b.as_ul(), """Instrument:
-First name:
-Last name:
-Birthday:
-Haircut type: """)
+ self.assertHTMLEqual(b.as_ul(), """Instrument:
+First name:
+Last name:
+Birthday:
+Haircut type: """)
def test_forms_with_prefixes(self):
# Sometimes it's necessary to have multiple forms display on the same HTML page,
@@ -2104,23 +2132,23 @@ Password:
self.assertHTMLEqual(
p.as_ul(),
"""First name:
-
+
Last name:
-
+
Birthday:
-"""
+"""
)
self.assertHTMLEqual(
str(p['first_name']),
- ''
+ ''
)
self.assertHTMLEqual(
str(p['last_name']),
- ''
+ ''
)
self.assertHTMLEqual(
str(p['birthday']),
- ''
+ ''
)
self.assertEqual(p.errors, {})
self.assertTrue(p.is_valid())
@@ -2195,11 +2223,11 @@ Password:
self.assertHTMLEqual(
p.as_ul(),
"""First name:
-
+
Last name:
-
+
Birthday:
-"""
+"""
)
data = {
'foo-prefix-first_name': 'John',
@@ -2232,37 +2260,37 @@ Password:
is_cool = NullBooleanField()
p = Person({'name': 'Joe'}, auto_id=False)
- self.assertHTMLEqual(str(p['is_cool']), """
+ self.assertHTMLEqual(str(p['is_cool']), """
""")
p = Person({'name': 'Joe', 'is_cool': '1'}, auto_id=False)
- self.assertHTMLEqual(str(p['is_cool']), """
+ self.assertHTMLEqual(str(p['is_cool']), """
""")
p = Person({'name': 'Joe', 'is_cool': '2'}, auto_id=False)
- self.assertHTMLEqual(str(p['is_cool']), """
+ self.assertHTMLEqual(str(p['is_cool']), """
""")
p = Person({'name': 'Joe', 'is_cool': '3'}, auto_id=False)
- self.assertHTMLEqual(str(p['is_cool']), """
+ self.assertHTMLEqual(str(p['is_cool']), """
""")
p = Person({'name': 'Joe', 'is_cool': True}, auto_id=False)
- self.assertHTMLEqual(str(p['is_cool']), """
+ self.assertHTMLEqual(str(p['is_cool']), """
""")
p = Person({'name': 'Joe', 'is_cool': False}, auto_id=False)
- self.assertHTMLEqual(str(p['is_cool']), """
+ self.assertHTMLEqual(str(p['is_cool']), """
@@ -2275,14 +2303,17 @@ Password:
file1 = FileField()
f = FileForm(auto_id=False)
- self.assertHTMLEqual(f.as_table(), '| File1: | |
')
+ self.assertHTMLEqual(
+ f.as_table(),
+ '| File1: | |
',
+ )
f = FileForm(data={}, files={}, auto_id=False)
self.assertHTMLEqual(
f.as_table(),
'| File1: | '
''
- ' |
'
+ ''
)
f = FileForm(data={}, files={'file1': SimpleUploadedFile('name', b'')}, auto_id=False)
@@ -2290,7 +2321,7 @@ Password:
f.as_table(),
'| File1: | '
'- The submitted file is empty.
'
- ' |
'
+ ''
)
f = FileForm(data={}, files={'file1': 'something that is not a file'}, auto_id=False)
@@ -2299,16 +2330,22 @@ Password:
'| File1: | '
'- No file was submitted. Check the '
'encoding type on the form.
'
- ' |
'
+ ''
)
f = FileForm(data={}, files={'file1': SimpleUploadedFile('name', b'some content')}, auto_id=False)
- self.assertHTMLEqual(f.as_table(), '| File1: | |
')
+ self.assertHTMLEqual(
+ f.as_table(),
+ '| File1: | |
',
+ )
self.assertTrue(f.is_valid())
file1 = SimpleUploadedFile('我隻氣墊船裝滿晒鱔.txt', 'मेरी मँडराने वाली नाव सर्पमीनों से भरी ह'.encode('utf-8'))
f = FileForm(data={}, files={'file1': file1}, auto_id=False)
- self.assertHTMLEqual(f.as_table(), '| File1: | |
')
+ self.assertHTMLEqual(
+ f.as_table(),
+ '| File1: | |
',
+ )
def test_basic_processing_in_view(self):
class UserRegistration(Form):
@@ -2334,18 +2371,18 @@ Password:
t = Template(
''
+ '\n\n'
)
return t.render(Context({'form': form}))
# Case 1: GET (an empty form, with no errors).)
self.assertHTMLEqual(my_function('GET', {}), """""")
# Case 2: POST with erroneous data (a redisplayed form, with errors).)
self.assertHTMLEqual(
@@ -2355,11 +2392,11 @@ Password:
- Please make sure your passwords match.
|
| Username: |
- Ensure this value has at most 10 characters (it has 23).
- |
-| Password1: | |
-| Password2: | |
+
+| Password1: | |
+| Password2: | |
-
+
"""
)
# Case 3: POST with valid data (the success message).)
@@ -2392,23 +2429,23 @@ Password:
{{ form.username.errors.as_ul }}Your username: {{ form.username }}
{{ form.password1.errors.as_ul }}Password: {{ form.password1 }}
{{ form.password2.errors.as_ul }}Password (again): {{ form.password2 }}
-
+
''')
self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """""")
self.assertHTMLEqual(
t.render(Context({'form': UserRegistration({'username': 'django'}, auto_id=False)})),
""""""
)
@@ -2420,13 +2457,13 @@ Password:
{{ form.username.label }}: {{ form.username }}
{{ form.password1.label }}: {{ form.password1 }}
{{ form.password2.label }}: {{ form.password2 }}
-
+
''')
self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """""")
# User form.[field].label_tag to output a field's label with a tag
@@ -2437,19 +2474,22 @@ Password:
{{ form.username.label_tag }} {{ form.username }}
{{ form.password1.label_tag }} {{ form.password1 }}
{{ form.password2.label_tag }} {{ form.password2 }}
-
+
''')
self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """""")
self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id='id_%s')})), """""")
# User form.[field].help_text to output a field's help text. If the given field
@@ -2458,16 +2498,16 @@ Password:
{{ form.username.label_tag }} {{ form.username }}
{{ form.username.help_text }}
{{ form.password1.label_tag }} {{ form.password1 }}
{{ form.password2.label_tag }} {{ form.password2 }}
-
+
''')
self.assertHTMLEqual(
t.render(Context({'form': UserRegistration(auto_id=False)})),
""""""
)
self.assertEqual(
@@ -2483,17 +2523,17 @@ Good luck picking a username that doesn't already exist.
{{ form.username.errors.as_ul }}Your username: {{ form.username }}
{{ form.password1.errors.as_ul }}Password: {{ form.password1 }}
{{ form.password2.errors.as_ul }}Password (again): {{ form.password2 }}
-
+
''')
self.assertHTMLEqual(
t.render(Context({
'form': UserRegistration({'username': 'django', 'password1': 'foo', 'password2': 'bar'}, auto_id=False)
})),
""""""
)
t = Template('''''')
self.assertHTMLEqual(
t.render(Context({
@@ -2509,10 +2549,10 @@ Good luck picking a username that doesn't already exist.
})),
""""""
)
@@ -2579,7 +2619,7 @@ Good luck picking a username that doesn't already exist.
self.assertHTMLEqual(
MyForm().as_table(),
'| Field1: | '
- ''
+ ' | '
' |
'
)
@@ -2597,24 +2637,25 @@ Good luck picking a username that doesn't already exist.
self.assertHTMLEqual(
p.as_ul(),
"""
-Name:
-Is cool:
+Name:
+Is cool:
+
Email:
-Age: """
+Age: """
)
self.assertHTMLEqual(
p.as_p(),
"""
Name:
-
+
Is cool:
-
+
@@ -2622,7 +2663,7 @@ Good luck picking a username that doesn't already exist.
Email:
Age:
-
"""
+"""
)
self.assertHTMLEqual(
@@ -2630,9 +2671,9 @@ Good luck picking a username that doesn't already exist.
"""
| Name: |
- |
+
| Is cool: |
-
+|
@@ -2641,7 +2682,7 @@ Good luck picking a username that doesn't already exist.
| |
| Age: |
- |
"""
+"""
)
def test_label_has_required_css_class(self):
@@ -3007,7 +3048,7 @@ Good luck picking a username that doesn't already exist.
form = SomeForm()
self.assertHTMLEqual(
form.as_p(),
- ' custom'
+ '
custom'
''
'
'
)
@@ -3026,7 +3067,7 @@ Good luck picking a username that doesn't already exist.
return self._html_output(
normal_row='%(field)s %(field_name)s
',
error_row='%s',
- row_ender='
',
+ row_ender='
',
help_text_html=' %s',
errors_on_separate_row=True
)
@@ -3034,9 +3075,9 @@ Good luck picking a username that doesn't already exist.
form = SomeForm()
self.assertHTMLEqual(
form.as_p(),
- ' custom
\n'
+ ' custom
\n'
''
- '
'
+ '
'
)
def test_error_dict(self):
@@ -3156,14 +3197,14 @@ Good luck picking a username that doesn't already exist.
"""
- (Hidden field last_name) This field is required.
First name:
-
+
"""
)
self.assertHTMLEqual(
p.as_p(),
"""- (Hidden field last_name) This field is required.
First name:
-
+
"""
)
self.assertHTMLEqual(
@@ -3171,7 +3212,7 @@ Good luck picking a username that doesn't already exist.
"""
- (Hidden field last_name) This field is required.
|
| First name: |
-
+
|
"""
)
@@ -3193,9 +3234,9 @@ Good luck picking a username that doesn't already exist.
"""
First name:
-
+
Last name:
-"""
+"""
)
self.assertHTMLEqual(
p.non_field_errors().as_text(),
@@ -3205,17 +3246,17 @@ Good luck picking a username that doesn't already exist.
p.as_p(),
"""
First name:
-
+
Last name:
-
"""
+"""
)
self.assertHTMLEqual(
p.as_table(),
""" |
| First name: |
- |
+
| Last name: |
- |
"""
+"""
)
def test_errorlist_override(self):
@@ -3239,9 +3280,9 @@ Good luck picking a username that doesn't already exist.
f = CommentForm(data, auto_id=False, error_class=DivErrorList)
self.assertHTMLEqual(f.as_p(), """Name:
Enter a valid email address.
-Email:
+Email:
-Comment:
""")
+Comment:
""")
def test_baseform_repr(self):
"""
@@ -3341,3 +3382,95 @@ Good luck picking a username that doesn't already exist.
self.assertEqual(force_text(form), form.__html__())
self.assertTrue(hasattr(form['username'], '__html__'))
self.assertEqual(force_text(form['username']), form['username'].__html__())
+
+ def test_use_required_attribute_true(self):
+ class MyForm(Form):
+ use_required_attribute = True
+ f1 = CharField(max_length=30)
+ f2 = CharField(max_length=30, required=False)
+ f3 = CharField(widget=Textarea)
+ f4 = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')])
+
+ form = MyForm()
+ self.assertHTMLEqual(
+ form.as_p(),
+ 'F1:
'
+ 'F2:
'
+ 'F3:
'
+ 'F4: '
+ ''
+ ''
+ '
',
+ )
+ self.assertHTMLEqual(
+ form.as_ul(),
+ 'F1: '
+ ''
+ 'F2: '
+ 'F3: '
+ 'F4: '
+ ''
+ ''
+ '',
+ )
+ self.assertHTMLEqual(
+ form.as_table(),
+ '| F1: | '
+ ' |
'
+ '| F2: | '
+ ' |
'
+ '| F3: | '
+ ' |
'
+ '| F4: | '
+ ''
+ ''
+ ' |
',
+ )
+
+ def test_use_required_attribute_false(self):
+ class MyForm(Form):
+ use_required_attribute = False
+ f1 = CharField(max_length=30)
+ f2 = CharField(max_length=30, required=False)
+ f3 = CharField(widget=Textarea)
+ f4 = ChoiceField(choices=[('P', 'Python'), ('J', 'Java')])
+
+ form = MyForm()
+ self.assertHTMLEqual(
+ form.as_p(),
+ 'F1:
'
+ 'F2:
'
+ 'F3:
'
+ 'F4: '
+ ''
+ ''
+ '
',
+ )
+ self.assertHTMLEqual(
+ form.as_ul(),
+ 'F1: '
+ 'F2: '
+ 'F3: '
+ 'F4: '
+ ''
+ ''
+ '',
+ )
+ self.assertHTMLEqual(
+ form.as_table(),
+ '| F1: | '
+ ' |
'
+ '| F2: | '
+ ' |
'
+ '| F3: | |
'
+ '| F4: | '
+ ''
+ ''
+ ' |
',
+ )
diff --git a/tests/forms_tests/tests/test_regressions.py b/tests/forms_tests/tests/test_regressions.py
index 10e5b3f983..e183694234 100644
--- a/tests/forms_tests/tests/test_regressions.py
+++ b/tests/forms_tests/tests/test_regressions.py
@@ -24,8 +24,8 @@ class FormsRegressionsTestCase(TestCase):
self.assertHTMLEqual(
TestForm(auto_id=False).as_p(),
- 'F1:
\n'
- 'F2:
'
+ 'F1:
\n'
+ 'F2:
'
)
def test_regression_3600(self):
@@ -39,7 +39,7 @@ class FormsRegressionsTestCase(TestCase):
self.assertHTMLEqual(
f.as_p(),
'username:'
- '
'
+ ''
)
# Translations are done at rendering time, so multi-lingual apps can define forms)
@@ -47,13 +47,13 @@ class FormsRegressionsTestCase(TestCase):
self.assertHTMLEqual(
f.as_p(),
'Benutzername:'
- '
'
+ ''
)
with translation.override('pl'):
self.assertHTMLEqual(
f.as_p(),
'u\u017cytkownik:'
- '
'
+ ''
)
def test_regression_5216(self):
@@ -82,12 +82,12 @@ class FormsRegressionsTestCase(TestCase):
'\xc5\xf8\xdf:'
'
'
)
@@ -101,12 +101,12 @@ class FormsRegressionsTestCase(TestCase):
'\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.\n'
'\xc5\xf8\xdf:'
'
'
)
diff --git a/tests/forms_tests/tests/tests.py b/tests/forms_tests/tests/tests.py
index f1bd66fc6b..bc4aebd5c4 100644
--- a/tests/forms_tests/tests/tests.py
+++ b/tests/forms_tests/tests/tests.py
@@ -109,24 +109,24 @@ class ModelFormCallableModelDefault(TestCase):
ChoiceOptionModel.objects.create(id=3, name='option 3')
self.assertHTMLEqual(
ChoiceFieldForm().as_p(),
- """Choice:
+ """Choice:
-Choice int:
+Choice int:
Multi choice:
-
+
Multi choice int:
-
+
@@ -145,25 +145,25 @@ class ModelFormCallableModelDefault(TestCase):
'multi_choice': [obj2, obj3],
'multi_choice_int': ChoiceOptionModel.objects.exclude(name="default"),
}).as_p(),
- """Choice:
+ """Choice:
-Choice int:
+Choice int:
Multi choice:
-
+
Multi choice int:
-
+
@@ -308,7 +308,7 @@ class EmptyLabelTestCase(TestCase):
f = EmptyCharLabelChoiceForm()
self.assertHTMLEqual(
f.as_p(),
- """Name:
+ """Name:
Choice:
@@ -320,7 +320,7 @@ class EmptyLabelTestCase(TestCase):
f = EmptyCharLabelNoneChoiceForm()
self.assertHTMLEqual(
f.as_p(),
- """Name:
+ """Name:
Choice string w none:
@@ -350,7 +350,7 @@ class EmptyLabelTestCase(TestCase):
f = EmptyIntegerLabelChoiceForm()
self.assertHTMLEqual(
f.as_p(),
- """Name:
+ """Name:
Choice integer:
@@ -370,7 +370,7 @@ class EmptyLabelTestCase(TestCase):
self.assertHTMLEqual(
f.as_p(),
"""Name:
-
+
Choice integer:
@@ -384,7 +384,7 @@ class EmptyLabelTestCase(TestCase):
self.assertHTMLEqual(
f.as_p(),
"""Name:
-
+
Choice integer:
--
cgit v1.3