Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #16236 -- Added get_form_kwargs method to WizardView as a way t…

…o easily provide kwargs to each step form.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16398 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b5e7eab9fddb981e0e20a663c76604e7d0c9a42a 1 parent 3ee076b
@jezdez jezdez authored
View
23 django/contrib/formtools/wizard/tests/formtests.py
@@ -35,6 +35,12 @@ class Step2(forms.Form):
class Step3(forms.Form):
data = forms.CharField()
+class CustomKwargsStep1(Step1):
+
+ def __init__(self, test=None, *args, **kwargs):
+ self.test = test
+ return super(CustomKwargsStep1, self).__init__(*args, **kwargs)
+
class UserForm(forms.ModelForm):
class Meta:
model = User
@@ -48,6 +54,12 @@ def dispatch(self, request, *args, **kwargs):
response = super(TestWizard, self).dispatch(request, *args, **kwargs)
return response, self
+ def get_form_kwargs(self, step, *args, **kwargs):
+ kwargs = super(TestWizard, self).get_form_kwargs(step, *args, **kwargs)
+ if step == 'kwargs_test':
+ kwargs['test'] = True
+ return kwargs
+
class FormTests(TestCase):
def test_form_init(self):
testform = TestWizard.get_initkwargs([Step1, Step2])
@@ -102,6 +114,17 @@ def test_form_condition(self):
response, instance = testform(request)
self.assertEquals(instance.get_next_step(), 'step3')
+ def test_form_kwargs(self):
+ request = get_request()
+
+ testform = TestWizard.as_view([('start', Step1),
+ ('kwargs_test', CustomKwargsStep1)])
+ response, instance = testform(request)
+
+ self.assertEqual(instance.get_form_kwargs('start'), {})
+ self.assertEqual(instance.get_form_kwargs('kwargs_test'), {'test': True})
+ self.assertEqual(instance.get_form('kwargs_test').test, True)
+
def test_form_prefix(self):
request = get_request()
View
12 django/contrib/formtools/wizard/views.py
@@ -354,6 +354,13 @@ def get_form_instance(self, step):
"""
return self.instance_dict.get(step, None)
+ def get_form_kwargs(self, step=None):
+ """
+ Returns the keyword arguments for instantiating the form
+ (or formset) on given step.
+ """
+ return {}
+
def get_form(self, step=None, data=None, files=None):
"""
Constructs the form for a given `step`. If no `step` is defined, the
@@ -366,12 +373,13 @@ def get_form(self, step=None, data=None, files=None):
if step is None:
step = self.steps.current
# prepare the kwargs for the form instance.
- kwargs = {
+ kwargs = self.get_form_kwargs(step)
+ kwargs.update({
'data': data,
'files': files,
'prefix': self.get_form_prefix(step, self.form_list[step]),
'initial': self.get_form_initial(step),
- }
+ })
if issubclass(self.form_list[step], forms.ModelForm):
# If the form is based on ModelForm, add instance if available.
kwargs.update({'instance': self.get_form_instance(step)})
View
10 docs/ref/contrib/formtools/form-wizard.txt
@@ -271,6 +271,16 @@ Advanced ``WizardView`` methods
def get_form_initial(self, step):
return self.initial_dict.get(step, {})
+.. method:: WizardView.get_form_kwargs(step)
+
+ Returns a dictionary which will be used as the keyword arguments when
+ instantiating the form instance on given ``step``.
+
+ The default implementation::
+
+ def get_form_kwargs(self, step):
+ return {}
+
.. method:: WizardView.get_form_instance(step)
Returns a object which will be passed to the form for ``step`` as
Please sign in to comment.
Something went wrong with that request. Please try again.