Ticket #10557: form_wizard_prev_fields.diff
File form_wizard_prev_fields.diff, 3.6 KB (added by , 16 years ago) |
---|
-
django/contrib/formtools/wizard.py
8 8 9 9 from django import forms 10 10 from django.conf import settings 11 from django.forms.forms import BoundField 11 12 from django.http import Http404 12 13 from django.shortcuts import render_to_response 13 14 from django.template.context import RequestContext … … 102 103 old_data = request.POST 103 104 prev_fields = [] 104 105 if old_data: 105 hidden = forms.HiddenInput() 106 # Collect all data from previous steps and render it as HTML hidden fields. 106 # Collect all data from previous steps as bound fields. 107 107 for i in range(step): 108 108 old_form = self.get_form(i, old_data) 109 109 hash_name = 'hash_%s' % i 110 prev_fields.extend([bf.as_hidden() for bf in old_form]) 111 prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form)))) 112 return self.render_template(request, form, ''.join(prev_fields), step, context) 110 prev_fields.extend([bf for bf in old_form]) 111 hash_field = forms.Field(initial=old_data.get(hash_name, 112 self.security_hash(request, old_form))) 113 bf = BoundField(forms.Form(), hash_field, hash_name) 114 prev_fields.append(bf) 115 return self.render_template(request, form, prev_fields, step, context) 113 116 114 117 # METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ######################## 115 118 -
docs/ref/contrib/formtools/form-wizard.txt
141 141 Next, you'll need to create a template that renders the wizard's forms. By 142 142 default, every form uses a template called :file:`forms/wizard.html`. (You can 143 143 change this template name by overriding 144 :meth:`~django.contrib.formtools.wizard. .get_template()`, which is documented144 :meth:`~django.contrib.formtools.wizard.get_template()`, which is documented 145 145 below. This hook also allows you to use a different template for each form.) 146 146 147 147 This template expects the following context: … … 152 152 * ``step_count`` -- The total number of steps. 153 153 * ``form`` -- The :class:`~django.forms.forms.Form` instance for the 154 154 current step (either empty or with errors). 155 * ``previous_fields`` -- A string representing every previous data field, 156 plus hashes for completed forms, all in the form of hidden fields. Note 157 that you'll need to run this through the 158 :meth:`~django.template.defaultfilters.safe` template filter, to prevent 159 auto-escaping, because it's raw HTML. 155 * ``previous_fields`` -- A list containing every previous data field, 156 plus hashes for completed forms, all in the form of regular fields. Note 157 that you'll need to call :meth:`as_hidden` on each field to prevent 158 them from appearing in your forms. 160 159 161 160 It will also be passed any objects in :data:`extra_context`, which is a 162 161 dictionary you can specify that contains extra values to add to the context. … … 182 181 {{ form }} 183 182 </table> 184 183 <input type="hidden" name="{{ step_field }}" value="{{ step0 }}" /> 185 { { previous_fields|safe }}184 {% for field in previous_fields %}{{ field.as_hidden }}{% endfor %} 186 185 <input type="submit"> 187 186 </form> 188 187 {% endblock %}