Using django form wizard with allauth

67 views Asked by At

Currently my user sign up process is implemented with allauth. Does anyone have any experience of how to make this a multipage process e.g. with form wizard from formtools?

forms.py (stored at users/forms.py)


class UserCreationForm1(forms.UserCreationForm):

    error_message = forms.UserCreationForm.error_messages.update(
        {
            "duplicate_username": _(
                "This username has already been taken."
            )
        }
    )

    username = CharField(label='User Name',
                        widget=TextInput(attrs={'placeholder': 'User Name'})
    )

    class Meta(forms.UserCreationForm.Meta):
        model = User
        fields = ['username', 'email', 'title', 'first_name', 'last_name', 'country', ]
        field_order = ['username', 'email', 'title', 'first_name', 'last_name', 'country', ]
        
    
    def clean_username(self):
        
        username = self.cleaned_data["username"]
        if self.instance.username == username:
            return username
        try:
            User._default_manager.get(username=username)
        except User.DoesNotExist:
            return username
        raise ValidationError(
            self.error_messages["duplicate_username"]
        )

class UserCreationForm2(forms.UserCreationForm):

    class Meta(forms.UserCreationForm.Meta):
        model = User
        fields = ['occupation', 'password1', 'password2', 'terms']
        field_order = ['occupation', 'password1', 'password2', 'terms']
        

    def clean_terms(self):
        is_filled = self.cleaned_data['terms']
        if not is_filled:
            raise forms.ValidationError('This field is required')
        return is_filled  

For the views.py i then have

SIGNUP_FORMS = [('0', UserCreationForm),
                ('1', UserCreationForm2)]

TEMPLATES = {'0': 'account/signup_1.html',
             '1': 'account/signup_2.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, UserCreationForm):
                print('form1')
                user = form.save(self.request)

            elif isinstance(form, UserCreationForm2):
                userprofile = form.save(commit=False)
                user = self.request.user
                userprofile.user = user
                userprofile.save()
                print('form2')
        return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)

What I find though is that after completing the first form and clicking to continue to the second the user is redirected to the inbuilt allauth login: at accounts/signup/

Does anyone have any advice?

0

There are 0 answers