',
+ )
p = Person({"last_name": "Lennon"})
self.assertEqual(p.errors["first_name"], ["This field is required."])
@@ -439,6 +480,15 @@ class FormsTestCase(SimpleTestCase):
""",
)
+ self.assertHTMLEqual(
+ p.as_div(),
+ '',
+ )
def test_auto_id_true(self):
# If auto_id is any True value whose str() does not contain '%s', the "id"
@@ -746,6 +796,15 @@ class FormsTestCase(SimpleTestCase):
""",
)
+ # Need an auto_id to generate legend.
+ self.assertHTMLEqual(
+ f.render(f.template_name_div),
+ '
(Hidden field hidden_input) This field is required.
"
+ '',
+ )
def test_dynamic_construction(self):
# It's possible to construct a Form dynamically by adding to the self.fields
@@ -1893,6 +1994,13 @@ class FormsTestCase(SimpleTestCase):
""",
)
+ self.assertHTMLEqual(
+ p.as_div(),
+ '
First name:
'
+ '
Last name:
'
+ 'Birthday:
',
+ )
# With auto_id set, a HiddenInput still gets an ID, but it doesn't get a label.
p = Person(auto_id="id_%s")
@@ -1926,6 +2034,15 @@ class FormsTestCase(SimpleTestCase):
""",
)
+ self.assertHTMLEqual(
+ p.as_div(),
+ '
First name:
Last name:
Birthday:'
+ ''
+ '
',
+ )
# If a field with a HiddenInput has errors, the as_table() and as_ul() output
# will include the error message(s) with the text "(Hidden field [fieldname]) "
@@ -1976,6 +2093,15 @@ class FormsTestCase(SimpleTestCase):
""",
)
+ self.assertHTMLEqual(
+ p.as_div(),
+ '
(Hidden field hidden_text) This field '
+ 'is required.
First name:
Last name:
'
+ 'Birthday: '
+ '
',
+ )
# A corner case: It's possible for a form to have only HiddenInputs.
class TestForm(Form):
@@ -2811,6 +2937,13 @@ Options: ",
)
+ self.assertHTMLEqual(
+ form.render(form.template_name_div),
+ '
',
+ )
def test_only_hidden_fields(self):
# A form with *only* hidden fields that has errors is going to be very unusual.
diff --git a/tests/forms_tests/tests/test_formsets.py b/tests/forms_tests/tests/test_formsets.py
index afa7a8a33d..c713c85bfb 100644
--- a/tests/forms_tests/tests/test_formsets.py
+++ b/tests/forms_tests/tests/test_formsets.py
@@ -1597,6 +1597,18 @@ class FormsetAsTagTests(SimpleTestCase):
),
)
+ def test_as_div(self):
+ self.assertHTMLEqual(
+ self.formset.as_div(),
+ self.management_form_html
+ + (
+ "