def streamforms_form(context, slug, reference, action=".", **kwargs): """ Renders a form on the page. {% load streamforms_tags %} {% streamforms_form "the-form-slug" "some-unique-reference" "." %} """ try: form = Form.objects.get(slug=slug) block = WagtailFormBlock() # the context is a RequestContext, we need to turn it into a dict or # the blocks in wagtail will start to fail with dict(context) return block.render( block.to_python({ "form": form.pk, "form_action": action, "form_reference": reference }), context.flatten(), ) except Form.DoesNotExist: return mark_safe("")
def test_render_when_form_deleted(self): block = WagtailFormBlock() html = block.render( block.to_python({ "form": 100, "form_action": "/foo/", "form_reference": "some-ref" })) expected_html = "\n".join( ["<p>Sorry, this form has been deleted.</p>"]) self.assertHTMLEqual(html, expected_html)
def test_render_when_form_deleted(self): block = WagtailFormBlock() html = block.render(block.to_python({ 'form': 100, 'form_action': '/foo/', 'form_reference': 'some-ref' })) expected_html = '\n'.join([ '<p>Sorry, this form has been deleted.</p>', ]) self.assertHTMLEqual(html, expected_html)
def test_render(self): block = WagtailFormBlock() html = block.render(block.to_python({ 'form': self.form.pk, 'form_action': '/foo/', 'form_reference': 'some-ref' })) expected_html = '\n'.join([ '<h2>Basic Form</h2>', '<form action="/foo/" method="post" novalidate>', '<input id="id_form_id" name="form_id" type="hidden" value="%s">' % self.form.pk, '<input id="id_form_reference" name="form_reference" type="hidden" value="some-ref">', '<div class="field-row">', '<label for="id_name">Name</label>', '<input type="text" name="name" maxlength="255" required id="id_name" />', '<p class="help-text">Please enter your name</p>', '</div>', '<input type="submit" value="Submit">', '</form>', ]) self.assertHTMLEqual(html, expected_html)
def test_render(self): block = WagtailFormBlock() html = block.render( block.to_python({ "form": self.form.pk, "form_action": ".", "form_reference": "some-ref" })) expected_html = "\n".join([ "<h2>Basic Form</h2>", '<form action="." enctype="multipart/form-data" method="post" novalidate>', '<input type="hidden" name="hidden" id="id_hidden" />', '<input id="id_form_id" name="form_id" type="hidden" value="%s">' % self.form.pk, '<input id="id_form_reference" name="form_reference" type="hidden" value="some-ref">', '<div class="field-row">' '<label for="id_singleline">singleline</label>' '<input type="text" name="singleline" required id="id_singleline" />' '<p class="help-text">Help</p>' "</div>", '<div class="field-row">' '<label for="id_multiline">multiline</label>' '<textarea name="multiline" cols="40" rows="10" required id="id_multiline">' "</textarea>" '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_date">date</label>' '<input type="text" name="date" value="" required id="id_date" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_datetime">datetime</label>' '<input type="text" name="datetime" value="" required id="id_datetime" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_email">email</label>' '<input type="email" name="email" required id="id_email" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_url">url</label>' '<input type="url" name="url" required id="id_url" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_number">number</label>' '<input type="number" name="number" step="any" required id="id_number" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_dropdown">dropdown</label>' '<select name="dropdown" id="id_dropdown">' '<option value="Option 1">Option 1</option>' '<option value="Option 2">Option 2</option>' '<option value="Option 3">Option 3</option></select>' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_radio_0">radio</label>' '<ul id="id_radio">' '<li><label for="id_radio_0">' '<input type="radio" name="radio" value="Option 1" required id="id_radio_0" /> Option 1' "</label></li>" '<li><label for="id_radio_1">' '<input type="radio" name="radio" value="Option 2" required id="id_radio_1" /> Option 2' "</label></li>" '<li><label for="id_radio_2">' '<input type="radio" name="radio" value="Option 3" required id="id_radio_2" /> Option 3' "</label></li></ul>" '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' "<label>checkboxes</label>" '<ul id="id_checkboxes">' '<li><label for="id_checkboxes_0">' '<input type="checkbox" name="checkboxes" value="Option 1" id="id_checkboxes_0" /> Option 1' "</label></li>" '<li><label for="id_checkboxes_1">' '<input type="checkbox" name="checkboxes" value="Option 2" id="id_checkboxes_1" /> Option 2' "</label></li>" '<li><label for="id_checkboxes_2">' '<input type="checkbox" name="checkboxes" value="Option 3" id="id_checkboxes_2" /> Option 3' "</label></li></ul>" '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_checkbox">checkbox</label>' '<input type="checkbox" name="checkbox" required id="id_checkbox" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_singlefile">singlefile</label>' '<input type="file" name="singlefile" required id="id_singlefile" />' '<p class="help-text">Help</p>' "</div>" '<div class="field-row">' '<label for="id_multifile">multifile</label>' '<input type="file" name="multifile" multiple required id="id_multifile" />' '<p class="help-text">Help</p>' "</div>" '<input type="submit" value="Submit">' "</form>", ]) self.assertHTMLEqual(html, expected_html)