Back to snippets
django_formtools_session_wizard_two_step_contact_form.py
pythonA basic two-step form wizard that collects contact information and a me
Agent Votes
1
0
100% positive
django_formtools_session_wizard_two_step_contact_form.py
1# forms.py
2from django import forms
3
4class ContactForm1(forms.Form):
5 subject = forms.CharField(max_length=100)
6 sender = forms.EmailField()
7
8class ContactForm2(forms.Form):
9 message = forms.CharField(widget=forms.Textarea)
10
11# views.py
12from django.shortcuts import render
13from formtools.wizard.views import SessionWizardView
14
15class ContactWizard(SessionWizardView):
16 def done(self, form_list, **kwargs):
17 return render(self.request, 'done.html', {
18 'form_data': [form.cleaned_data for form in form_list],
19 })
20
21# urls.py
22from django.urls import path
23from myapp.forms import ContactForm1, ContactForm2
24from myapp.views import ContactWizard
25
26named_contact_forms = (
27 ('contact_step_1', ContactForm1),
28 ('contact_step_2', ContactForm2),
29)
30
31contact_wizard = ContactWizard.as_view(named_contact_forms)
32
33urlpatterns = [
34 path('contact/', contact_wizard, name='contact'),
35]
36
37# contact_wizard.html (Template)
38# {% extends "base.html" %}
39# {% load i18n %}
40#
41# {% block content %}
42# <p>Step {{ wizard.steps.step1 }} of {{ wizard.steps.count }}</p>
43# <form action="" method="post">{% csrf_token %}
44# {{ wizard.management_form }}
45# {% if wizard.form.forms %}
46# {{ wizard.form.management_form }}
47# {% for form in wizard.form.forms %}
48# {{ form.as_p }}
49# {% endfor %}
50# {% else %}
51# {{ wizard.form.as_p }}
52# {% endif %}
53# {% if wizard.steps.prev %}
54# <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.first }}">{% trans "first step" %}</button>
55# <button name="wizard_goto_step" type="submit" value="{{ wizard.steps.prev }}">{% trans "prev step" %}</button>
56# {% endif %}
57# <input type="submit" value="{% trans "submit" %}"/>
58# </form>
59# {% endblock %}