Code

Ticket #16236: formwizard_form_initkwargs.diff

File formwizard_form_initkwargs.diff, 2.8 KB (added by zuko, 3 years ago)
Line 
1diff --git a/django/contrib/formtools/wizard/tests/formtests.py b/django/contrib/formtools/wizard/tests/formtests.py
2index 111981f..93f6b5f 100644
3--- a/django/contrib/formtools/wizard/tests/formtests.py
4+++ b/django/contrib/formtools/wizard/tests/formtests.py
5@@ -35,6 +35,12 @@ class Step2(forms.Form):
6 class Step3(forms.Form):
7     data = forms.CharField()
8 
9+class CustomKwargsForm(forms.Form):
10+    name = forms.CharField()
11+
12+    def __init__(self, test=None, *args, **kwargs):
13+        return super(CustomKwargsForm, self).__init__(*args, **kwargs)
14+
15 class UserForm(forms.ModelForm):
16     class Meta:
17         model = User
18@@ -48,6 +54,12 @@ class TestWizard(WizardView):
19         response = super(TestWizard, self).dispatch(request, *args, **kwargs)
20         return response, self
21 
22+    def get_form_kwargs(self, step, **kwargs):
23+        kwargs = super(TestWizard, self).get_form_kwargs(step, **kwargs)
24+        if step == 'kwargs_test':
25+            kwargs['test'] = True
26+        return kwargs
27+
28 class FormTests(TestCase):
29     def test_form_init(self):
30         testform = TestWizard.get_initkwargs([Step1, Step2])
31@@ -102,6 +114,16 @@ class FormTests(TestCase):
32         response, instance = testform(request)
33         self.assertEquals(instance.get_next_step(), 'step3')
34 
35+    def test_form_kwargs(self):
36+        request = get_request()
37+
38+        testform = TestWizard.as_view([('start', Step1),
39+            ('kwargs_test', CustomKwargsForm)])
40+        response, instance = testform(request)
41+
42+        self.assertTrue('test' not in instance.get_form_kwargs('start'))
43+        self.assertTrue('test' in instance.get_form_kwargs('kwargs_test'))
44+
45     def test_form_prefix(self):
46         request = get_request()
47 
48diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
49index ac9cfbd..f458900 100644
50--- a/django/contrib/formtools/wizard/views.py
51+++ b/django/contrib/formtools/wizard/views.py
52@@ -366,6 +366,14 @@ class WizardView(TemplateView):
53         if step is None:
54             step = self.steps.current
55         # prepare the kwargs for the form instance.
56+        kwargs = self.get_form_kwargs(step, data=data, files=files)
57+        return self.form_list[step](**kwargs)
58+
59+    def get_form_kwargs(self, step, data=None, files=None):
60+        """
61+        Returns the keyword arguments for instantiating the form (or formset)
62+        on given step.
63+        """
64         kwargs = {
65             'data': data,
66             'files': files,
67@@ -378,7 +386,7 @@ class WizardView(TemplateView):
68         elif issubclass(self.form_list[step], forms.models.BaseModelFormSet):
69             # If the form is based on ModelFormSet, add queryset if available.
70             kwargs.update({'queryset': self.get_form_instance(step)})
71-        return self.form_list[step](**kwargs)
72+        return kwargs
73 
74     def process_step(self, form):
75         """