Ticket #10557: trac-10557.2.diff
File trac-10557.2.diff, 6.6 KB (added by , 14 years ago) |
---|
-
django/contrib/formtools/tests/__init__.py
diff --git a/django/contrib/formtools/tests/__init__.py b/django/contrib/formtools/tests/__init__.py index 9c7d46f..b194845 100644
a b class WizardTests(TestCase): 360 360 "wizard_step": "1"} 361 361 wizard(DummyRequest(POST=data)) 362 362 363 def test_prev_fields_as_hidden(self): 364 """ 365 Previous fields should be available as hidden fields. 366 """ 367 data = {"0-field": "test", 368 "1-field": "test2", 369 "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c", 370 "wizard_step": "1"} 371 372 previous_fields = '<input type="hidden" name="0-field" value="test" id="id_0-field" /><input type="hidden" name="hash_0" value="7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c" id="id_hash_0" /><input type="hidden" name="1-field" value="test2" id="id_1-field" /><input type="hidden" name="hash_1" value="d5b434e3934cc92fee4bd2964c4ebc06f81d362d" id="id_hash_1" />' 373 374 response = self.client.post('/wizard/', data) 375 self.assertEquals(2, response.context['step0']) 376 self.assertEquals(previous_fields, response.context['previous_fields']) 377 378 def test_prev_fields_as_list(self): 379 """ 380 Previous fields should be available as hidden fields. 381 """ 382 data = {"0-field": "test", 383 "1-field": "test2", 384 "hash_0": "7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c", 385 "wizard_step": "1"} 386 387 response = self.client.post('/wizard/', data) 388 self.assertEquals(2, response.context['step0']) 389 390 the_fields = [(f.html_name, f.data if f.form.is_bound else f.form.initial.get(f.name, f.field.initial)) for f in response.context['previous_fields_list']] 391 392 self.assertEquals(the_fields, [('0-field', u'test'), ('hash_0', u'7e9cea465f6a10a6fb47fcea65cb9a76350c9a5c'), ('1-field', u'test2'), ('hash_1', 'd5b434e3934cc92fee4bd2964c4ebc06f81d362d')]) 393 -
django/contrib/formtools/wizard.py
diff --git a/django/contrib/formtools/wizard.py b/django/contrib/formtools/wizard.py index d382e29..a1c6af2 100644
a b stored on the server side. 5 5 """ 6 6 7 7 import cPickle as pickle 8 import inspect 8 9 9 10 from django import forms 10 11 from django.conf import settings 11 12 from django.contrib.formtools.utils import security_hash, form_hmac 13 from django.forms.forms import BoundField 12 14 from django.http import Http404 13 15 from django.shortcuts import render_to_response 14 16 from django.template.context import RequestContext … … class FormWizard(object): 145 147 prev_fields = [] 146 148 if old_data: 147 149 hidden = forms.HiddenInput() 148 # Collect all data from previous steps and render it as HTML hidden fields.150 # Collect all data from previous steps 149 151 for i in range(step): 150 152 old_form = self.get_form(i, old_data) 151 153 hash_name = 'hash_%s' % i 152 prev_fields.extend([bf.as_hidden() for bf in old_form]) 153 prev_fields.append(hidden.render(hash_name, old_data.get(hash_name, self.security_hash(request, old_form)))) 154 return self.render_template(request, form, ''.join(prev_fields), step, context) 154 prev_fields.extend([bf for bf in old_form]) 155 hash_field = forms.Field( 156 initial=old_data.get( 157 hash_name, 158 self.security_hash(request, old_form))) 159 hash_bf = BoundField(forms.Form(), hash_field, hash_name) 160 prev_fields.append(hash_bf) 161 162 # render the previous fields as HTML hidden fields. 163 html_prev_fields = ''.join([f.as_hidden() for f in prev_fields]) 164 165 method_args = inspect.getargspec(self.render_template).args 166 if 'previous_fields_list' in method_args: 167 return self.render_template(request, form, html_prev_fields, 168 step, context, 169 previous_fields_list=prev_fields) 170 else: 171 return self.render_template(request, form, html_prev_fields, 172 step, context) 155 173 156 174 # METHODS SUBCLASSES MIGHT OVERRIDE IF APPROPRIATE ######################## 157 175 … … class FormWizard(object): 223 241 """ 224 242 return 'forms/wizard.html' 225 243 226 def render_template(self, request, form, previous_fields, step, context=None): 244 def render_template(self, request, form, previous_fields, 245 step, context=None, previous_fields_list=None): 227 246 """ 228 247 Renders the template for the given step, returning an HttpResponse object. 229 248 … … class FormWizard(object): 243 262 hidden fields. Note that you'll need to run this 244 263 through the "safe" template filter, to prevent 245 264 auto-escaping, because it's raw HTML. 265 previous_fields_list -- A list containing every previous data 266 field, plus hashes for completed forms, all in the 267 form of regular fields. Note that you'll need to 268 call :meth:`as_hidden` on each field to prevent 269 them from appearing in your forms. 246 270 """ 247 271 context = context or {} 248 272 context.update(self.extra_context) … … class FormWizard(object): 252 276 step=step + 1, 253 277 step_count=self.num_steps(), 254 278 form=form, 255 previous_fields=previous_fields 279 previous_fields=previous_fields, 280 previous_fields_list=previous_fields_list 256 281 ), context_instance=RequestContext(request)) 257 282 258 283 def process_step(self, request, form, step): -
docs/ref/contrib/formtools/form-wizard.txt
diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt index 370fbdc..653e27e 100644
a b This template expects the following context: 152 152 plus hashes for completed forms, all in the form of hidden fields. Note 153 153 that you'll need to run this through the :tfilter:`safe` template filter, 154 154 to prevent auto-escaping, because it's raw HTML. 155 ' ``previous_fields_list`` -- 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 them 158 from appearing in your forms. 155 159 156 160 You can supply extra context to this template in two ways: 157 161