django-formtools,在表单步骤之间保存数据

时间:2019-08-15 18:38:38

标签: django django-formtools

我正在尝试使用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(表单有效)时,表单步骤没有保存。波纹管就是一个例子

第一个表格已加载:

8e66r.png

点击“下一步”后:

8eQnO.png

现在,当我单击“上一个”时,一切都消失了:

8eMqq.png

我想知道我错过了什么?

0 个答案:

没有答案