Django Form Wizard - preview before submission

Hi! I hope all of you are safe.

I have successfully implemented a wizard-type form using the form tools wizard package. I would like to use the other package in the form tools library for previewing forms to preview the Wizard that I had created.

There’s no mention of the integration between the two modules from either docs, and previous Stackoverflow posts are not giving much help either.

My current plan is to utilize the context_data from the wizard and just render the preview via JS on the template itself like this:

class MyWizard(SessionWizardView):
    def get_context_data(self, **kwargs):
        context = super().get_context_data(**kwargs)
        context['wizard_data'] = self.get_all_cleaned_data()
        # Render a usable version of {{ wizard_data }} in the template 
        return context 

I thought it’s a good idea to ask around for other ways to accomplish this task. Any help or bump to the right direction is appreciated. Thank you!

I’m not sure there’s a direct link between the two.

I’m guessing that what you’re wanting to do is have someone fill out all the forms of the wizard and then present one preview view.

The issue is that the preview tool redisplays the form that was used to enter data - which isn’t part of the wizard set. (Unless you’re looking to do the preview of each form in the wizard, which is a different issue, and easily solved by making each individual form in the wizard a preview-form.)

1 Like

I see. Thank you for the explanation. Now that I thought about it, perhaps I could implement the preview portion by rendering a template within the done() method of the Wizard. Something like this:

class MyWizard:
    def done(self, form_list, *args, **kwargs):
        preview_data = extract_preview_data(form_list)
        return render(self.request, 'preview.html', {'preview_data': preview_data})

def actually_save_the_data(request):
    # The old contents of the done method above, waiting for a POST request from preview.html that signifies:
    # "The preview looks good, go ahead and save it!"
    ...

Quick edit: just realized that the done() method is in response of a POST request, so a render might not be the best idea. I probably need to figure out a way to pass the preview_data after a redirect.

I don’t see anything wrong with that idea. The POST just means that you’ve gotten to that point as the result of a form submission, and so you want to display what you can effectively think of as another form before redirecting. That “other form” may be nothing more than a display page with two buttons “Accept” or “Return to form” - the former performing the redirect you would otherwise have done, the latter to set you back into the wizard.