如何使用多个模板实现Django Session Wizard
How to implement Django Session Wizard with multiple templates
我正在尝试使用 django-formtools 附带的表单向导在 django 中实现多步骤表单,到目前为止,它会遍历所有表单直到最后一个表单,而不是调用 done
方法,该页面返回到第一个表单。我的一些代码如下所示:
Urls.py
from django.urls import path
from minereg import views
urlpatterns = [
path('',views.ContactWizard.as_view(views.FORMS),name="registration"),
]
Views.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")
done
方法中的 print(form_data)
没有发生,表明代码永远不会到达那个点。
home.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 %}
bank.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 %}
forms.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)
希望有人能看到我遗漏了什么。
刚刚在 home.html
code.Got 中发现了问题,通过将行 <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}"> Next step </button>
更改为 <button name="wizard_goto_step" type="submit"> Next step </button>
它可以正常工作
我正在尝试使用 django-formtools 附带的表单向导在 django 中实现多步骤表单,到目前为止,它会遍历所有表单直到最后一个表单,而不是调用 done
方法,该页面返回到第一个表单。我的一些代码如下所示:
Urls.py
from django.urls import path
from minereg import views
urlpatterns = [
path('',views.ContactWizard.as_view(views.FORMS),name="registration"),
]
Views.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")
done
方法中的 print(form_data)
没有发生,表明代码永远不会到达那个点。
home.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 %}
bank.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 %}
forms.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)
希望有人能看到我遗漏了什么。
刚刚在 home.html
code.Got 中发现了问题,通过将行 <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}"> Next step </button>
更改为 <button name="wizard_goto_step" type="submit"> Next step </button>