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:
saving each form data into a database, (this has been done by overriding the process_step() of the SessionView class)
allow the user to navigate to any earlier form and load the form with the data entered by the user.
provide a navigation so that the user can pick and choose which form they would like to fill and submit
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
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.