Ticket #16236: formwizard_form_initkwargs.diff

File formwizard_form_initkwargs.diff, 2.8 KB (added by Anton Strogonoff, 13 years ago)
  • django/contrib/formtools/wizard/tests/formtests.py

    diff --git a/django/contrib/formtools/wizard/tests/formtests.py b/django/contrib/formtools/wizard/tests/formtests.py
    index 111981f..93f6b5f 100644
    a b class Step2(forms.Form):  
    3535class Step3(forms.Form):
    3636    data = forms.CharField()
    3737
     38class CustomKwargsForm(forms.Form):
     39    name = forms.CharField()
     40
     41    def __init__(self, test=None, *args, **kwargs):
     42        return super(CustomKwargsForm, self).__init__(*args, **kwargs)
     43
    3844class UserForm(forms.ModelForm):
    3945    class Meta:
    4046        model = User
    class TestWizard(WizardView):  
    4854        response = super(TestWizard, self).dispatch(request, *args, **kwargs)
    4955        return response, self
    5056
     57    def get_form_kwargs(self, step, **kwargs):
     58        kwargs = super(TestWizard, self).get_form_kwargs(step, **kwargs)
     59        if step == 'kwargs_test':
     60            kwargs['test'] = True
     61        return kwargs
     62
    5163class FormTests(TestCase):
    5264    def test_form_init(self):
    5365        testform = TestWizard.get_initkwargs([Step1, Step2])
    class FormTests(TestCase):  
    102114        response, instance = testform(request)
    103115        self.assertEquals(instance.get_next_step(), 'step3')
    104116
     117    def test_form_kwargs(self):
     118        request = get_request()
     119
     120        testform = TestWizard.as_view([('start', Step1),
     121            ('kwargs_test', CustomKwargsForm)])
     122        response, instance = testform(request)
     123
     124        self.assertTrue('test' not in instance.get_form_kwargs('start'))
     125        self.assertTrue('test' in instance.get_form_kwargs('kwargs_test'))
     126
    105127    def test_form_prefix(self):
    106128        request = get_request()
    107129
  • django/contrib/formtools/wizard/views.py

    diff --git a/django/contrib/formtools/wizard/views.py b/django/contrib/formtools/wizard/views.py
    index ac9cfbd..f458900 100644
    a b class WizardView(TemplateView):  
    366366        if step is None:
    367367            step = self.steps.current
    368368        # prepare the kwargs for the form instance.
     369        kwargs = self.get_form_kwargs(step, data=data, files=files)
     370        return self.form_list[step](**kwargs)
     371
     372    def get_form_kwargs(self, step, data=None, files=None):
     373        """
     374        Returns the keyword arguments for instantiating the form (or formset)
     375        on given step.
     376        """
    369377        kwargs = {
    370378            'data': data,
    371379            'files': files,
    class WizardView(TemplateView):  
    378386        elif issubclass(self.form_list[step], forms.models.BaseModelFormSet):
    379387            # If the form is based on ModelFormSet, add queryset if available.
    380388            kwargs.update({'queryset': self.get_form_instance(step)})
    381         return self.form_list[step](**kwargs)
     389        return kwargs
    382390
    383391    def process_step(self, form):
    384392        """
Back to Top