Django formtools done function not executed

回眸只為那壹抹淺笑 提交于 2019-12-11 14:52:27

问题


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.



来源:https://stackoverflow.com/questions/56635382/django-formtools-done-function-not-executed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!