FormWizard and FileFields (Django 1.4)

后端 未结 1 1873
执念已碎
执念已碎 2021-01-03 03:05

My FormWizard (Django 1.4) allows the user to step back and forth until he completes the wizard. The wizard keeps all the values the user filled in and displays them in case

1条回答
  •  执念已碎
    2021-01-03 03:45

    I recently run into the same problem, and could solve it by subclassing Django's SessionWizardView (in my case NamedUrlSessionWizardView), and overriding the get_form method.

    Basicly I do the the following:

    • Get the files that are already stored for the step.
    • Iterate over the current submitted files.
    • If a submitted file is None, ignore it, else overwrite the already stored value.

    Here is the code:

    from django.contrib.formtools.wizard.views import NamedUrlSessionWizardView
    
    class MyWizardView(NamedUrlSessionWizardView):
    
        def get_form(self, step=None, data=None, files=None):
            if step:
                step_files = self.storage.get_step_files(step)
            else:
                step_files = self.storage.current_step_files
    
            if step_files and files:
                for key, value in step_files.items():
                    if files.has_key(key) and files[key] is not None:
                        step_files[key] = files[key]
            elif files:
                step_files = files
    
            return super(MyWizardView, self).get_form(step, data, step_files)
    

    0 讨论(0)
提交回复
热议问题