Ticket #12074: formset-as_p-as_ul-with-unittests.patch

File formset-as_p-as_ul-with-unittests.patch, 3.9 KB (added by dpn, 5 years ago)
  • django/forms/formsets.py

    diff -r 8c064fcf7778 django/forms/formsets.py
    a b  
    328328        forms = u' '.join([form.as_table() for form in self.forms])
    329329        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
    330330
     331    def as_p(self):
     332        "Returns this formset rendered as HTML <p>s."
     333        forms = u' '.join([form.as_p() for form in self.forms])
     334        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
     335
     336    def as_ul(self):
     337        "Returns this formset rendered as HTML <li>s."
     338        forms = u' '.join([form.as_ul() for form in self.forms])
     339        return mark_safe(u'\n'.join([unicode(self.management_form), forms]))
     340
    331341def formset_factory(form, formset=BaseFormSet, extra=1, can_order=False,
    332342                    can_delete=False, max_num=None):
    333343    """Return a FormSet for the given form class."""
  • tests/regressiontests/forms/formsets.py

    diff -r 8c064fcf7778 tests/regressiontests/forms/formsets.py
    a b  
    11# -*- coding: utf-8 -*-
     2from django.test.testcases import TestCase
     3from django.forms.forms import Form
     4from django.forms.fields import CharField, IntegerField
     5from django.forms.formsets import formset_factory
    26tests = """
    37# Basic FormSet creation and usage ############################################
    48
     
    722726<ul class="errorlist"><li>You may only specify a drink once.</li></ul>
    723727
    724728"""
     729
     730data = {
     731    'choices-TOTAL_FORMS': '1', # the number of forms rendered
     732    'choices-INITIAL_FORMS': '0', # the number of forms with initial data
     733    'choices-MAX_NUM_FORMS': '0', # max number of forms
     734    'choices-0-choice': 'Calexico',
     735    'choices-0-votes': '100',
     736}
     737
     738class Choice(Form):
     739    choice = CharField()
     740    votes = IntegerField()
     741   
     742ChoiceFormSet = formset_factory(Choice)
     743
     744class FormsetAsFooTests(TestCase):
     745    def test_as_table(self):
     746        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
     747        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" />
     748<tr><th>Choice:</th><td><input type="text" name="choices-0-choice" value="Calexico" /></td></tr>
     749<tr><th>Votes:</th><td><input type="text" name="choices-0-votes" value="100" /></td></tr>""")
     750   
     751    def test_as_p(self):
     752        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
     753        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" />
     754<p>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></p>
     755<p>Votes: <input type="text" name="choices-0-votes" value="100" /></p>""")
     756       
     757    def test_as_ul(self):
     758        formset = ChoiceFormSet(data, auto_id=False, prefix='choices')
     759        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" />
     760<li>Choice: <input type="text" name="choices-0-choice" value="Calexico" /></li>
     761<li>Votes: <input type="text" name="choices-0-votes" value="100" /></li>""")
     762       
  • tests/regressiontests/forms/tests.py

    diff -r 8c064fcf7778 tests/regressiontests/forms/tests.py
    a b  
    3737from formsets import tests as formset_tests
    3838from media import media_tests
    3939
     40
     41from formsets import FormsetAsFooTests
    4042from fields import FieldsTests
    4143from validators import TestFieldWithValidators
    4244from widgets import WidgetTests
Back to Top