Code

Ticket #10557: form_wizard_prev_fields.diff

File form_wizard_prev_fields.diff, 3.6 KB (added by Matthew Flanagan <mattimustang@…>, 5 years ago)

patch for class and docs

Line 
1Index: django/contrib/formtools/wizard.py
2===================================================================
3--- django/contrib/formtools/wizard.py  (revision 10088)
4+++ django/contrib/formtools/wizard.py  (working copy)
5@@ -8,6 +8,7 @@
6 
7 from django import forms
8 from django.conf import settings
9+from django.forms.forms import BoundField
10 from django.http import Http404
11 from django.shortcuts import render_to_response
12 from django.template.context import RequestContext
13@@ -102,14 +103,16 @@
14         old_data = request.POST
15         prev_fields = []
16         if old_data:
17-            hidden = forms.HiddenInput()
18-            # Collect all data from previous steps and render it as HTML hidden fields.
19+            # Collect all data from previous steps as bound fields.
20             for i in range(step):
21                 old_form = self.get_form(i, old_data)
22                 hash_name = 'hash_%s' % i
23-                prev_fields.extend([bf.as_hidden() for bf in old_form])
24-                prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form))))
25-        return self.render_template(request, form, ''.join(prev_fields), step, context)
26+                prev_fields.extend([bf for bf in old_form])
27+                hash_field = forms.Field(initial=old_data.get(hash_name,
28+                    self.security_hash(request, old_form)))
29+                bf = BoundField(forms.Form(), hash_field, hash_name)
30+                prev_fields.append(bf)
31+        return self.render_template(request, form, prev_fields, step, context)
32 
33     # METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ########################
34
35Index: docs/ref/contrib/formtools/form-wizard.txt
36===================================================================
37--- docs/ref/contrib/formtools/form-wizard.txt  (revision 10088)
38+++ docs/ref/contrib/formtools/form-wizard.txt  (working copy)
39@@ -141,7 +141,7 @@
40 Next, you'll need to create a template that renders the wizard's forms. By
41 default, every form uses a template called :file:`forms/wizard.html`. (You can
42 change this template name by overriding
43-:meth:`~django.contrib.formtools.wizard..get_template()`, which is documented
44+:meth:`~django.contrib.formtools.wizard.get_template()`, which is documented
45 below. This hook also allows you to use a different template for each form.)
46 
47 This template expects the following context:
48@@ -152,11 +152,10 @@
49     * ``step_count`` -- The total number of steps.
50     * ``form`` -- The :class:`~django.forms.forms.Form` instance for the
51       current step (either empty or with errors).
52-    * ``previous_fields`` -- A string representing every previous data field,
53-      plus hashes for completed forms, all in the form of hidden fields. Note
54-      that you'll need to run this through the
55-      :meth:`~django.template.defaultfilters.safe` template filter, to prevent
56-      auto-escaping, because it's raw HTML.
57+    * ``previous_fields`` -- A list containing every previous data field,
58+      plus hashes for completed forms, all in the form of regular fields. Note
59+      that you'll need to call :meth:`as_hidden` on each field to prevent
60+      them from appearing in your forms.
61 
62 It will also be passed any objects in :data:`extra_context`, which is a
63 dictionary you can specify that contains extra values to add to the context.
64@@ -182,7 +181,7 @@
65     {{ form }}
66     </table>
67     <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" />
68-    {{ previous_fields|safe }}
69+    {% for field in previous_fields %}{{ field.as_hidden }}{% endfor %}
70     <input type="submit">
71     </form>
72     {% endblock %}