Loading django/forms/formsets.py +10 −0 Original line number Diff line number Diff line Loading @@ -326,6 +326,16 @@ class BaseFormSet(StrAndUnicode): forms = u' '.join([form.as_table() for form in self.forms]) return mark_safe(u'\n'.join([unicode(self.management_form), forms])) def as_p(self): "Returns this formset rendered as HTML <p>s." forms = u' '.join([form.as_p() for form in self.forms]) return mark_safe(u'\n'.join([unicode(self.management_form), forms])) def as_ul(self): "Returns this formset rendered as HTML <li>s." forms = u' '.join([form.as_ul() for form in self.forms]) return mark_safe(u'\n'.join([unicode(self.management_form), forms])) def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False, can_delete=False, max_num=None): """Return a FormSet for the given form class.""" Loading tests/regressiontests/forms/formsets.py +38 −0 Original line number Diff line number Diff line # -*- coding: utf-8 -*- from django.test.testcases import TestCase from django.forms.forms import Form from django.forms.fields import CharField, IntegerField from django.forms.formsets import formset_factory tests = """ # Basic FormSet creation and usage ############################################ Loading Loading @@ -722,3 +726,37 @@ False <ul class="errorlist"><li>You may only specify a drink once.</li></ul> """ data = { 'choices-TOTAL_FORMS': '1', # the number of forms rendered 'choices-INITIAL_FORMS': '0', # the number of forms with initial data 'choices-MAX_NUM_FORMS': '0', # max number of forms 'choices-0-choice': 'Calexico', 'choices-0-votes': '100', } class Choice(Form): choice = CharField() votes = IntegerField() ChoiceFormSet = formset_factory(Choice) class FormsetAsFooTests(TestCase): def test_as_table(self): formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertEqual(formset.as_table(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" value="Calexico" /></td></tr> <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" value="100" /></td></tr>""") def test_as_p(self): formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertEqual(formset.as_p(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> <p>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></p> <p>Votes: <input type="text" name="choices-0-votes" value="100" /></p>""") def test_as_ul(self): formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertEqual(formset.as_ul(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> <li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li>""") tests/regressiontests/forms/tests.py +2 −0 Original line number Diff line number Diff line Loading @@ -38,6 +38,8 @@ from widgets import tests as widgets_tests from formsets import tests as formset_tests from media import media_tests from formsets import FormsetAsFooTests from fields import FieldsTests from validators import TestFieldWithValidators from widgets import WidgetTests, ClearableFileInputTests Loading Loading
django/forms/formsets.py +10 −0 Original line number Diff line number Diff line Loading @@ -326,6 +326,16 @@ class BaseFormSet(StrAndUnicode): forms = u' '.join([form.as_table() for form in self.forms]) return mark_safe(u'\n'.join([unicode(self.management_form), forms])) def as_p(self): "Returns this formset rendered as HTML <p>s." forms = u' '.join([form.as_p() for form in self.forms]) return mark_safe(u'\n'.join([unicode(self.management_form), forms])) def as_ul(self): "Returns this formset rendered as HTML <li>s." forms = u' '.join([form.as_ul() for form in self.forms]) return mark_safe(u'\n'.join([unicode(self.management_form), forms])) def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False, can_delete=False, max_num=None): """Return a FormSet for the given form class.""" Loading
tests/regressiontests/forms/formsets.py +38 −0 Original line number Diff line number Diff line # -*- coding: utf-8 -*- from django.test.testcases import TestCase from django.forms.forms import Form from django.forms.fields import CharField, IntegerField from django.forms.formsets import formset_factory tests = """ # Basic FormSet creation and usage ############################################ Loading Loading @@ -722,3 +726,37 @@ False <ul class="errorlist"><li>You may only specify a drink once.</li></ul> """ data = { 'choices-TOTAL_FORMS': '1', # the number of forms rendered 'choices-INITIAL_FORMS': '0', # the number of forms with initial data 'choices-MAX_NUM_FORMS': '0', # max number of forms 'choices-0-choice': 'Calexico', 'choices-0-votes': '100', } class Choice(Form): choice = CharField() votes = IntegerField() ChoiceFormSet = formset_factory(Choice) class FormsetAsFooTests(TestCase): def test_as_table(self): formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertEqual(formset.as_table(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> <tr><th>Choice:</th><td><input type="text" name="choices-0-choice" value="Calexico" /></td></tr> <tr><th>Votes:</th><td><input type="text" name="choices-0-votes" value="100" /></td></tr>""") def test_as_p(self): formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertEqual(formset.as_p(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> <p>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></p> <p>Votes: <input type="text" name="choices-0-votes" value="100" /></p>""") def test_as_ul(self): formset = ChoiceFormSet(data, auto_id=False, prefix='choices') self.assertEqual(formset.as_ul(),"""<input type="hidden" name="choices-TOTAL_FORMS" value="1" /><input type="hidden" name="choices-INITIAL_FORMS" value="0" /><input type="hidden" name="choices-MAX_NUM_FORMS" value="0" /> <li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li> <li>Votes: <input type="text" name="choices-0-votes" value="100" /></li>""")
tests/regressiontests/forms/tests.py +2 −0 Original line number Diff line number Diff line Loading @@ -38,6 +38,8 @@ from widgets import tests as widgets_tests from formsets import tests as formset_tests from media import media_tests from formsets import FormsetAsFooTests from fields import FieldsTests from validators import TestFieldWithValidators from widgets import WidgetTests, ClearableFileInputTests Loading