Django formwizard: transferring data between forms

I have a form wizard containing 3 forms. Basically, I am trying to transfer data from the first and second forms to the third. I tried to add a dictionary attribute to the wizard class and update this dictionary every time the process_step method is called. The Django 1.4 documentation says that this method is called every time a page is displayed for all the steps presented.

In the following code example, the dictionary attribute is changed using the integer self.test to keep it simple. In this case, every time the process_step method is called, the value of self.test is 2, never increases. The __init__ method seems to be invoked for each form.

 class MyWizard(SessionWizardView): def __init__(self, *args, **kwargs): super(MyWizard, self).__init__(*args, **kwargs) self.test = 1 def process_step(self, form): self.test += 1 print self.test return self.get_form_step_data(form) 

Besides this solution, is there a more elegant way to transfer data between forms of a form wizard?

+4
source share
3 answers

You can do this with sessions submitting "inital" info in 3rd form. Here is an example of something similar .

+1
source

I would do the following:

 class MyWizard(SessionWizardView): def get_context_data(self, form, **kwargs): context = super(MyWizard, self).get_context_data(form=form, **kwargs) if self.steps.step1 == 3: data_from_step_1 = self.get_cleaned_data_for_step('0') # zero indexed data_from_step_2 = self.get_cleaned_data_for_step('1') # zero indexed context.update({'data_from_step_1':data_from_step_1, 'data_from_step_2':data_from_step_2}) return context 
+8
source

I have little experience with formwizard, but from django docs this one looks like you.

 def get_context_data(self, form, **kwargs): context = super(MyWizard, self).get_context_data(form=form, **kwargs) if self.steps.current == 'my_step_name': context.update({'another_var': True}) return context 
+2
source

All Articles