I have a django form wizard. Before a user enters the wizard and at each of the 4 steps of the wizard, I want to evaluate an if condition to check for data.
When the if condition fails, I want to exit the wizard and redirect the user to the start page, where an info block is displayed telling the user of the issue of no data.
I have tried placing the if condition to redirect the user in the wizard, but I have been advised that I can't return a redirect in get_context_data(), since the SessionWizardView won't return that value as a response.
Is there a way around this issue?
Here is my form wizard code:
class CreateDocumentWizard(SessionWizardView):
template_name = 'documents/document_create.html'
form_list = [
core_forms.CreateDocumentWizardForm01,
core_forms.CreateDocumentWizardForm02,
core_forms.CreateDocumentWizardForm03,
core_forms.CreateDocumentWizardForm04,
]
def get_form_kwargs(self, step=None):
kwargs = super(CreateDocumentWizard, self).get_form_kwargs(step)
kwargs.setdefault('user', self.request.user)
return kwargs
def get_context_data(self, form, **kwargs):
name_details_count = NameDetails.objects.filter(user=self.request.user).count()
# redirect user if no name details held.
if name_details_count < 1:
return HttpResponseRedirect('document_start')
You can hook into render()
or even get()/post()/dispatch()
methods:
class CreateDocumentWizard(SessionWizardView):
...
def render(self, form=None, **kwargs):
name_details_count = NameDetails.objects.filter(user=self.request.user).count()
# redirect user if no name details held.
if name_details_count < 1:
return HttpResponseRedirect('document_start')
return super(CreateDocumentWizard, self).render(form, **kwargs)