django-formtools Documentation, Release dev
(continued from previous page)
path('contact/', ContactWizard.as_view([ContactForm1, ContactForm2])),
]
You can also pass the form list as a class attribute named form_list:
class ContactWizard(WizardView):
form_list = [ContactForm1, ContactForm2]
2.2.5 Using a different template for each form
As mentioned above, you may specify a different template for each form. Consider an example using a form wizard to
implement a multi-step checkout process for an online store. In the first step, the user specifies a billing and shipping
address. In the second step, the user chooses payment type. If they chose to pay by credit card, they will enter credit
card information in the next step. In the final step, they will confirm the purchase.
Here’s what the view code might look like:
from django.http import HttpResponseRedirect
from formtools.wizard.views import SessionWizardView
FORMS = [("address", myapp.forms.AddressForm),
("paytype", myapp.forms.PaymentChoiceForm),
("cc", myapp.forms.CreditCardForm),
("confirmation", myapp.forms.OrderForm)]
TEMPLATES = {"address": "checkout/billingaddress.html",
"paytype": "checkout/paymentmethod.html",
"cc": "checkout/creditcard.html",
"confirmation": "checkout/confirmation.html"}
def pay_by_credit_card(wizard):
"""Return true if user opts to pay by credit card"""
# Get cleaned data from payment step
cleaned_data = wizard.get_cleaned_data_for_step('paytype') or {'method': 'none'}
# Return true if the user selected credit card
return cleaned_data['method'] == 'cc'
class OrderWizard(SessionWizardView):
def get_template_names(self):
return [TEMPLATES[self.steps.current]]
def done(self, form_list,
**
kwargs):
do_something_with_the_form_data(form_list)
return HttpResponseRedirect('/page-to-redirect-to-when-done/')
...
The urls.py file would contain something like:
urlpatterns = [
path('checkout/', OrderWizard.as_view(FORMS, condition_dict={'cc': pay_by_credit_
˓→card})),
]
The condition_dict can be passed as attribute for the as_view() method or as a class attribute named
condition_dict:
2.2. Usage 11