gbozee
gbozee

Reputation: 4796

Using a form wizard as Signup form django allauth

I am trying to implement a form wizard at the registration/signup process. I am using django-allauth for authentication and based on the docs and a previous question How to customize user profile when using django-allauth It describes how to add extra fields to the sign up form. I don't really see how I can override the default form to use a form wizard. One option I was considering is adding all the extra fields to the signup form then displaying section of the forms with ajax but I am not sure how to implement validation on the different sections. Any guidance or help on how to implement the registration step as a wizard would be greatly appreciated.

Upvotes: 0

Views: 1000

Answers (2)

user3588162
user3588162

Reputation: 116

I recently did this by:

views.py

from allauth.account.forms import SignupForm
from allauth.account.utils import complete_signup

SIGNUP_FORMS = [('signup', SignupForm),
                ('profile', forms.UserProfileForm)]

TEMPLATES = {'signup': 'trips/forms/wizard_form.html',
             'profile': 'trips/forms/wizard_form.html'}

class SignupWizard(SessionWizardView):
    def get_template_names(self):
        return [TEMPLATES[self.steps.current]]

    def done(self, form_list, **kwargs):
        for form in form_list:
            if isinstance(form, SignupForm):
                user = form.save(self.request)
                complete_signup(self.request, user, settings.ACCOUNT_EMAIL_VERIFICATION, settings.LOGIN_REDIRECT_URL)
            elif isinstance(form, forms.UserProfileForm):
                userprofile = form.save(commit=False)
                user = self.request.user
                userprofile.user = user
                userprofile.save()
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)

You can add as many forms as you want. In my case, the UserProfileForm is a ModelForm that creates a new UserProfile object with a one-to-one relationship to the User. Both objects are only saved after both forms are submitted successfully. The complete_signup function is from allauth and it does some cleanup and then logs the user in to the site.

Upvotes: 1

gbozee
gbozee

Reputation: 4796

I ended up implementing the Wizard from the client side using AngularJS Django-angular package and this library. After digging through the allauth signup view code, I figured out it already implemented an AjaxCapableProcessFormViewMixin

Implementing a wizard using client side code for the sign up process when using django-allauth is probably the best way to go since you can delay the successful redirection till all forms in the wizard are filled and also prevents splitting long signup forms into smaller forms.

Upvotes: 0

Related Questions