我正在尝试使用django-formtools创建一个多步骤表单向导。我有一个名为的主模型Report
和几个 ModelForms,如下所示:
class ReportFormP1(forms.ModelForm):
class Meta:
model = Report
fields = [
'company',
'address_line_1',
'address_line_2',
'responsible_person',
'person_consulted',
'assessor_name',
'previous_assessment_date',
'review_date',
]
# section 1
class ReportFormP2(forms.ModelForm):
class Meta:
model = Report
fields = [
"premise_num_floors",
"premise_floor_area",
"premise_occupancy",
"premise_occupancy_comments",
]
在我看来,我创建了以下视图:
类 ReportWizardView(SessionWizardView): template_name = 'reporting/report_create2.html' form_list = [ReportFormP1, ReportFormP2]
def done(self, form_list, **kwargs):
return render(self.request, 'done.html', {
'form_data': [form.cleaned_data for form in form_list],
})
我的相关模板如下所示:
{% extends '_base.html' %}
{% load static %}
{% load i18n %}
{% load crispy_forms_tags %}
<!-- templates/home.html -->
{% load socialaccount %}
{% block title %}Create a report{% endblock title %}
{% block extra_css %}
{{ wizard.form.media }}
{% endblock extra_css %}
{% block content %}
<div class="container">
<h1>Create a report</h1>
<p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
<form action="" method="post">
{% csrf_token %}
{{ wizard.management_form }}
{% if wizard.form.forms %}
{{ wizard.form.management_form }}
{% for form in wizard.form.forms %}
{{ form|crispy }}
{% endfor %}
{% else %}
{{ wizard.form|crispy }}
{% endif %}
<div class="text-center">
{% if wizard.steps.prev %}
<button class="btn btn-lg btn-warning" name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}" formnovalidate>{% trans "first step" %}</button>
<button class="btn btn-lg btn-primary" name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}" formnovalidate>{% trans "prev step" %}</button>
{% endif %}
{% if wizard.steps.next %}
<button class="btn btn-lg btn-primary" name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}">{% trans "next step" %}</button>
{% endif %}
<input class="btn btn-lg btn-success" type="submit" name="Submit" value="Create report">
</div>
</form>
<hr>
<hr>
</div>
{% endblock content %}
我的 urls.py 如下所示:
path('create_report2', ReportWizardView.as_view(), name="report_create2"),
我的问题是当我点击时表单步骤没有被保存next
,然后previous
(表单有效)。贝娄是一个例子
加载的第一个表单:
点击“下一步”后:
现在,当我单击“上一个”时,一切都消失了:
我想知道我错过了什么?