我尝试使用 django-formtools 附带的表单向导在 django 中实现多步骤表单,到目前为止,它会逐步遍历所有表单,直到最后一个表单,然后不是调用该done
方法,而是页面返回到第一个表单。我的一些代码如下所示:
网址.py
from django.urls import path
from minereg import views
urlpatterns = [
path('',views.ContactWizard.as_view(views.FORMS),name="registration"),
]
视图.py
from django.shortcuts import render
from formtools.wizard.views import SessionWizardView
from .forms import RegForm1,RegForm2
FORMS = [("home",RegForm1),("bank",RegForm2)]
TEMPLATES = {"home":"home.html",
"bank":"bank.html"}
class ContactWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self,form_list, **kwargs): #Required
form_data = [form.cleaned_data for form in form_list]
print(form_data)
return render(self.request,template_name = "thanks.html")
方法中的print(form_data)
indone
没有发生,显示代码永远不会到达那一点。
主页.html
{% extends "base.html" %}
{% load widget_tweaks %}
{% load i18n %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="{%url 'registration' %}" method='post'>
{% csrf_token %}
{{wizard.management_form}}
<div class="container col-md-9">
<h1>PERSONAL DETAILS</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<!-- Name -->
<div class="row">
<div class="form-group col-md-4">
{{ wizard.form.name.label_tag }}
{{ wizard.form.name|add_class:"form-control form-control-lg" }}
</div>
<div class="form-group col-md-4">
{{ wizard.form.middle_name.label_tag }}
{{ wizard.form.middle_name|add_class:"form-control form-control-lg" }}
</div>
<div class="form-group col-md-4">
{{ wizard.form.last_name.label_tag }}
{{ wizard.form.last_name|add_class:"form-control form-control-lg" }}
</div>
</div>
<!-- DOB -->
<div class="row">
<div class="form-group col-md-4">
{{ wizard.form.date_of_birth.label_tag }}
{{ wizard.form.date_of_birth|add_class:"form-control form-control-lg" }}
</div>
</div>
<!-- EMAIL & NUMBER -->
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.email.label_tag }}
{{ wizard.form.email|add_class:"form-control form-control-lg" }}
</div>
</div>
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.mobile_number.label_tag }}
{{ wizard.form.mobile_number|add_class:"form-control form-control-lg" }}
</div>
<!-- ADDRESS -->
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.address.label_tag }}
{{ wizard.form.address|add_class:"form-control form-control-lg" }}
</div>
</div>
<div class="row">
<div class="form-group col-md-4">
{{ wizard.form.suburb.label_tag }}
{{ wizard.form.suburb|add_class:"form-control form-control-lg" }}
</div>
<div class="form-group col-md-4">
{{ wizard.form.state.label_tag }}
{{ wizard.form.state|add_class:"form-control form-control-lg" }}
</div>
<div class="form-group col-md-4">
{{ wizard.form.postcode.label_tag }}
{{ wizard.form.postcode|add_class:"form-control form-control-lg" }}
</div>
</div>
<hr>
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
<button name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}"> Next step </button>
</form>
{% endblock %}
银行.html
{% extends "base.html" %}
{% load widget_tweaks %}
{% load i18n %}
{% block content %}
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="{%url 'registration' %}" method='post'>
{% csrf_token %}
{{wizard.management_form}}
<div class="container col-md-9">
<h1>BANKING AND SUPERANNUATION DETAILS</h1>
<p>Please fill in this form to create an account.</p>
<hr>
<!-- ACCOUNT NAME -->
<div class="row">
<div class="form-group col-md-4">
{{ wizard.form.account_name.label_tag }}
{{ wizard.form.account_name|add_class:"form-control form-control-lg" }}
</div>
</div>
<!-- BSB -->
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.bsb.label_tag }}
{{ wizard.form.bsb|add_class:"form-control form-control-lg" }}
</div>
</div>
<!-- ACCOUNT NUMBER -->
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.account_number.label_tag }}
{{ wizard.form.account_number|add_class:"form-control form-control-lg" }}
</div>
</div>
<!-- FUND NAME -->
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.fund_name.label_tag }}
{{ wizard.form.fund_name|add_class:"form-control form-control-lg" }}
</div>
</div>
<!-- MEMBER NUMBER -->
<div class="row">
<div class="form-group col-md-8">
{{ wizard.form.member_number.label_tag }}
{{ wizard.form.member_number|add_class:"form-control form-control-lg" }}
</div>
</div>
</div>
<hr>
<p>By creating an account you agree to our <a href="#">Terms & Privacy</a>.</p>
<input type="submit" value="{{ wizard.steps.next }}"/>
</form>
{% endblock %}
表格.py
from django import forms
class RegForm1(forms.Form):
name = forms.CharField(max_length=50)
middle_name = forms.CharField(max_length=50)
last_name = forms.CharField(max_length=50)
date_of_birth = forms.CharField(max_length=50)
email = forms.CharField(max_length=50)
mobile_number = forms.CharField(max_length=50)
address = forms.CharField(max_length=50)
suburb = forms.CharField(max_length=50)
state = forms.CharField(max_length=50)
postcode = forms.CharField(max_length=50)
class RegForm2(forms.Form):
account_name = forms.CharField(max_length=50)
bsb = forms.CharField(max_length=50)
account_number = forms.CharField(max_length=50)
fund_name = forms.CharField(max_length=50)
member_number = forms.CharField(max_length=50)
希望有人能看到我错过了什么。