diff --git a/django/contrib/formtools/wizard/tests/formtests.py b/django/contrib/formtools/wizard/tests/formtests.py index 111981fea2..6b3cc3da43 100644 --- a/django/contrib/formtools/wizard/tests/formtests.py +++ b/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 @@ class TestWizard(WizardView): 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 @@ class FormTests(TestCase): 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() diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py index ac9cfbdb10..2dab77d0fe 100644 --- a/django/contrib/formtools/wizard/views.py +++ b/django/contrib/formtools/wizard/views.py @@ -354,6 +354,13 @@ class WizardView(TemplateView): """ 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 @@ class WizardView(TemplateView): 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)}) diff --git a/docs/ref/contrib/formtools/form-wizard.txt b/docs/ref/contrib/formtools/form-wizard.txt index 44d29eebce..b0e685e5a9 100644 --- a/docs/ref/contrib/formtools/form-wizard.txt +++ b/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