Backing up and restoring the Django Form Wizard

I have a little problem understanding and applying the backup and restore functions for the form wizard module from jango contrib libraries.

I tried to create a storage class that uses the database to store the current state, not a cookie or session, but this did not solve my case, because when the form is initialized, all the old data is cleared, the same in both cookie stores and session storage (and mine was a cookie mimic one, but saves db)

The problem that I need to fill out is that if the user left the form in step x.th (let's say that the electric one exited), and then log in again and run the wizard, the user must continue until completion or click β€œcancel”, (which has not yet been implemented).

+4
source share
1 answer

You can override the post method in the view as follows:

class YourWizardView(SessionWizardView):

    def post(self, *args, **kwargs):
        form = self.get_form(
            data=self.request.POST, files=self.request.FILES)
        data = self.get_all_cleaned_data()
        form.is_valid()  # to generate cleaned data
        data.update(form.cleaned_data)
        #Save your data HERE
        return super(YourWizardView, self).post(*args, **kwargs)
     def get_form_instance(self, step):
        # Getting obj instance values
        obj = None
        if 'pk' in self.kwargs:
            obj = get_object_or_404(
                YourModel, pk=self.kwargs['pk'])
        return self.instance_dict.get(step, obj)

If you want the user to go back and look through the data without clicking further, you can do javascript to send data via ajax (for example, every 5 seconds)

+1
source

Source: https://habr.com/ru/post/1547745/


All Articles