Django formtools完成函数未执行 [英] Django formtools done function not executed
问题描述
我正在尝试在Django中实现formwizard.这些步骤按预期方式工作,但是在提交了最终表单(第二步)之后,未完成函数.我找不到原因.请直接.以下是我的代码,
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')
我想在第一步提交后发送带有otp的邮件,然后在第二步中,我要求第一步中发送的otp进行验证.默认情况下,提交最后一步后,页面将重定向到步骤1.为什么?
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?
推荐答案
我在上面找到了我的查询的答案.由于process_step(),似乎未执行完.我只是评论了process_step()函数,并执行了done()函数.猜猜我将不得不更深入地研究它.
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.
这篇关于Django formtools完成函数未执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!