Django formtools done function not executed

2019-08-21 08:04发布

问题:

I am trying to implement the formwizard in django. The steps work as expected, but aftet the final form (second step) is submitted, the done function is not hit. I am not able to find the reason why. Kindly direct. Following is my code,

Forms.py

from django import forms

class FormStepOne(forms.Form):
    name = forms.CharField(max_length=100)
    last_name = forms.CharField(max_length=100)
    phone = forms.CharField(max_length=100)
    email = forms.EmailField()

class FormStepTwo(forms.Form):
    otp = forms.CharField(max_length=100)

views.py

from django.shortcuts import render
from .forms import FormStepOne, FormStepTwo
from formtools.wizard.views import SessionWizardView
from django.http import HttpResponseRedirect

# Create your views here.
class FormWizardView(SessionWizardView):
    template_name = 'done.html'
    form_list = [FormStepOne, FormStepTwo]

    def done(self, form_list, **kwargs):
        print('done')
        return render(self.request, 'home.html', {'form_data':[form.cleaned_data for form in form_list],})
        # return HttpResponseRedirect('/home/')

    def process_step(self, form):
        # print(self.get_form_step_data(form)['0-email'])
        # print(self.steps.current)
        if self.steps.current == '0':
            print('send mail to ' + self.get_form_step_data(form)['0-email'])
        else:
            print('verify the entered otp')

I want to send a mail with otp after 1st step is submitted, and then in the second step I ask for the otp that was sent in first step for verification. And by default, after submitting the last step the page is redirected to step 1. Why?

回答1:

I found the answer to my query above. It seems the done wasn't executing due to the process_step(). I just commented the process_step() function and the done() function got executed. Guess I will have to dive deeper into it.