pythondjangodjango-formwizarddjango-formtools

How to implement Django Session Wizard with multiple templates


Im trying to implement a multi step form in django using the form wizard that comes with django-formtools and so far it steps through all the forms till the last one then instead of calling the done method, the page goes back to the first form. Some of my code is shown below:

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

The print(form_data) in the done method doesn't happen, showing the code never gets to that point.

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)

Hopefully someone can see what I'm missing.


Solution

  • Just found the problem in the home.html code.Got it working by changing the line <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.next }}"> Next step </button> to <button name="wizard_goto_step" type="submit"> Next step </button>