In the following code, the words render and POST print on every form page generation and post submission respectively. process_step only prints on the final page submission and the done()
method never fires, even to throw an exception.
Any attempt to retrieve form data always returns an empty MultiValueDict
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('wizard/',views.ListingWizard.as_view(views.FORMS), name='listing.wizard'),
]
forms.py
from django import forms
from .models import Listing
class LandlordApprovalForm(forms.Form):
is_landlord_approved = forms.BooleanField(required=True,
label="Has your landlord given you permission to sublet your unit?",
initial=False,
widget=forms.RadioSelect(choices=[(True, 'Yes'), (False, 'No')]))
class DateForm(forms.Form):
start_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
end_date = forms.DateField(widget=forms.DateInput(attrs={'type': 'date'}))
class RentForm(forms.Form):
monthly_rent = forms.IntegerField(label='What is your portion of the monthly rent?')
total_rent = forms.IntegerField(label="What is the total monthly rent for the unit among all tenants?")
desired_rent = forms.IntegerField(label="What are you looking to receive in rent?")
views.py
from django.shortcuts import redirect,render
from formtools.wizard.views import SessionWizardView
from .forms import DateForm, LandlordApprovalForm, RentForm
from .models import Listing
FORMS = [
('landlordApproval', LandlordApprovalForm),
('availabilityDates', DateForm),
('rent', RentForm)]
TEMPLATES = {
'landlordApproval': 'listing/landlord.html',
'availabilityDates': 'listing/date.html',
'rent': 'listing/rent.html'
}
class ListingWizard(SessionWizardView):
template_data = {
'landlordApproval': {'form_header': 'Before we begin...'},
'availabilityDates': {'form_header': 'Tell us about your place...'},
'rent': {'form_header': 'Tell us about your place...'}
}
template_name = 'form_base.html'
def get_context_data(self, form, **kwargs):
context = super().get_context_data(form, **kwargs)
context['template_data'] = self.template_data[self.steps.current]
return context
def render(self, form, **kwargs):
print("render")
if self.request.method == 'POST':
print('POST')
prev_form = self.get_form_step_data(self.get_form(self.steps.prev))
print(prev_form)
return super().render(form, **kwargs)
def process_step(self, form, **kwargs):
print(form)
print("process step")
return super().process_step(form, **kwargs)
def done(self, form_list, **kwargs):
print("done")
form_base.html
{% extends "base.html" %}
{% block content %}
<div class="modal-content flex justify-center items-center">
<div class="bg-white rounded-lg shadow-lg p-4 w-7/8 md:w-1/3 lg:w-1/3 mt-8">
<div class="text-center font-poppins text-lg md:text-2xl font-bold text-spothue mb-4">
{{ template_data.form_header }}
</div>
<form method="POST">
{% csrf_token %}
{{ wizard.management_form }}
{{ wizard.form }}
<div class="flex justify-between">
<div class="w-1/2 text-left">
{% if wizard.steps.prev %}
<button type='submit' name='wizard_goto_step' value='{{ wizard.steps.prev }}' class="text-lg">
<i class="text-spothue bi bi-arrow-left-circle-fill"></i>
<span>Previous</span>
</button>
{% endif %}
</div>
<div class="w-1/2 text-right">
{% if wizard.steps.next %}
<button type='submit'name='wizard_goto_step' value='{{ wizard.steps.next }}' class="text-lg">
<span>Next</span>
<i class="text-spothue bi bi-arrow-right-circle-fill"></i>
</button>
{% else %}
<button type='submit' class=" bg-spothue border-yellow-400 border-2 text-white rounded-md px-2 py-1 text-lg">
Submit
</button>
{% endif %}
</div>
</div>
</form>
</div>
</div>
{% endblock %}