django formtools, allow user to select which form they want to interact with

I am trying to create Wizard to collect some information from my users. I am using django-formtools.

The functionality i want in the wizard are:

  1. saving each form data into a database, (this has been done by overriding the process_step() of the SessionView class)
  2. allow the user to navigate to any earlier form and load the form with the data entered by the user.
  3. provide a navigation so that the user can pick and choose which form they would like to fill and submit
  4. if user abandons the wizard after filling up a few forms, they should be able to come back to the wizard and continue
from django.shortcuts import redirect
from .forms import Step1Form, Step2Form, Step3Form  # Replace these with your actual form classes

class OrangeSimulationWizardView(SessionWizardView):
    template_name = 'path/to/template.html'  # Replace with your actual template path
    form_list = [
        ('step1', Step1Form),
        ('step2', Step2Form),
        ('step3', Step3Form),
    ]

    def get_form_kwargs(self, step=None):
        kwargs = super().get_form_kwargs(step)
        # Customize form kwargs if needed, based on the step
        return kwargs

    def get_context_data(self, form, **kwargs):
        context = super().get_context_data(form=form, **kwargs)
        # Add any additional context here
        return context

    def done(self, form_list, **kwargs):
        # Handle completion of the wizard (e.g., save data) or redirect
        return redirect('some-view-name')  # Replace with your desired redirect target

How can i do this ?

Welcome @s13rw81 !

This isn’t really a primary use-case for that tool.

The docs describe the form wizard:

The form wizard application splits forms across multiple Web pages. It maintains state in one of the backends so that the full server-side processing can be delayed until the submission of the final form.

In the situation where you’re working with multiple different forms and a purely arbitrary sequence of events, you’re actually better off creating your own sequence of views and navigation to handle this.

Greetings, thank you for taking the time to disucss this issue.

I was partially able to acheive some of the functionality by over riding render in the following manner

    # def render(self, form = None, **kwargs):
    #
    #     # Check session for NEXT_STEP
    #     next_step = self.request.session.get('NEXT_STEP', None)
    #     project_id = self.request.session.get('SESSION_PROJECT_ID', None)
    #
    #     # If NEXT_STEP exists and is not the current step, redirect to it
    #     if project_id and next_step and next_step != self.steps.current:
    #         return self.render_goto_step(next_step)
    #
    #     # Proceed with the usual rendering if there's no step to jump to
    #     return super().render(form = form, **kwargs)

however the back button stopped working and the wizard would start over from the first form page instead of going to the results page.

I will repeat what Ken said: This is just a bunch of pages, not a wizard.

You said “thanks for taking the time to discuss this issue” while, it seems to me, that you totally ignored what Ken said.

I will then repeat again: you are describing multiple pages that write to the db individually, and that the user can move freely between maybe via some kind of menu. This is not a wizard.

i am sorry that my reply did not fully express that i have read the full comment and have understood and agree with what @KenWhitesell said.

by posting my code which worked (partially) I was merely looking for any suggestions (missing piece ) from the community to solve this puzzle.

Sadly in my case I do not think there is none. Again thank you for taking the time to discuss this.

TLDR: i was looking for some closure.

1 Like

I just one tiny clarification, is it possible to rig the Wizard to begin from a Form which is not the first form and then go serially till the end.

for example, if there are 7 forms, is it possible to start at form 3 and then go to the 4th, 5th … till the end ?

Sure. You can write any code you want. Just point the link to page two. It’s not really a wizard anyway, like we said.