如何用多个模板实现Django会话向导?

问题描述 投票:0回答:1

我试图在django中使用django-formtools自带的表单向导来实现一个多步骤的表单,到目前为止,它通过所有的表单步骤,直到最后一个,然后不调用 done 方法,页面回到第一个表单。我的一些代码如下所示。

Urls.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")

视图.py print(form_data)done 方法不会发生,显示代码从未到达那个点。

首页.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)

希望有人能看出我的不足。

python django django-formwizard django-formtools
1个回答
0
投票

刚刚发现问题的地方 home.html 通过修改代码中的 <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}"> Next step </button><button name="wizard_goto_step" type="submit"> Next step </button>

© www.soinside.com 2019 - 2024. All rights reserved.