def test_value_from_datadict(self): formset = formset_factory(ContactsForm) widget = widgets.ContactsWidget(attrs={'formset': formset}) data = { 'contacts-TOTAL_FORMS': '2', 'contacts-INITIAL_FORMS': '1', 'contacts-MIN_NUM_FORMS': '0', 'contacts-MAX_NUM_FORMS': '1000', 'contacts-0-name': 'Ringo', 'contacts-0-email': '*****@*****.**', 'contacts-0-tel': '555-555', 'contacts-1-name': '', 'contacts-1-email': '', 'contacts-1-tel': '' } q_dict = QueryDict('', mutable=True) q_dict.update(data) value = widget.value_from_datadict(q_dict, {}, 'contacts') assert isinstance(value, formset)
def test_render_with_formset(self): value = [{ 'name': 'Ringo', 'email': '*****@*****.**', 'tel': '555-555', }] formset = formset_factory(ContactsForm) value = formset(initial=value, prefix='contacts') for form in value.forms: for field in form: field.field.widget.attrs = {'some': 'attr'} widget = widgets.ContactsWidget(attrs={'formset': formset}) html = widget.render('contacts', value, attrs={'class': 'some'}) assert ('<table class="table contacts-form">' ' <thead>' ' <tr>' ' <th>Name</th>' ' <th>Email</th>' ' <th>Phone</th>' ' <th></th>' ' </tr>' ' </thead>' in html) assert (' <tfoot>' ' <tr>' ' <td colspan="4">' ' <button data-prefix="contacts" type="button" ' ' class="btn btn-default btn-sm" ' ' id="add-contact"><span class="glyphicon ' ' glyphicon-plus" aria-hidden="true"></span> ' ' Add contact</button>' ' </td>' ' </tr>' ' </tfoot>' '</table>' in html) assert 'contacts-0-name' in html assert 'contacts-1-name' in html