André
André

Reputation: 25584

Django - How to do a multi page form?

I'm doing a Signup process with 3 steps,

1 - (Getting Started), Basic Info
2 - (Checkout), Payment
3 - (Profile Details), choose user and password

I've been goggling but I'm not sure what is the best process to do this.

Should I track the data by generating a UUID on the URL?

http://some.com/signup/123kslk434435
http://some.com/signup/checkout/123kslk434435
http://some.com/signup/create-account/123kslk434435

Or should I do this using sessions?

What the best way of doing it?

Best Regards, André

Upvotes: 0

Views: 1519

Answers (1)

Shaun O'Keefe
Shaun O'Keefe

Reputation: 1338

The Django Form Wizard is probably what you're after. This will handle creating a session for you (using the SessionWizardView) and then present you at the end of the workflow with each of the completed forms, which you can then process, save to objects etc. as you see fit.

Create each of your forms for each step as such:

from django import forms

class BasicInfoForm(forms.Form):
    info_field_1 = forms.CharField(max_length=100)
    ...

class CheckoutForm(forms.Form):
    checkout_field_1 = forms.CharField(max_length=100)

...

Then create your view, which will process your forms once they're all done. Be sure to redirect at the end.

from django.http import HttpResponseRedirect
from django.contrib.formtools.wizard.views import SessionWizardView

class SignupWizard(SessionWizardView):
    def done(self, form_list, **kwargs):
        (process each of your forms, which are contained in form list)
        return HttpResponseRedirect('/page-to-redirect-to-when-done/')

And then connect up the forms with the view in your urlconfig

urlpatterns = [
    url(r'^contact/$', ContactWizard.as_view([BasicInfoForm, CheckoutForm, ...]))
]

See the documentation for creating templates, how to process form_list etc.

Upvotes: 1

Related Questions