Django conditional forms help

Has anyone used the django form wizard? I’m having some trouble using it
I have this code for multipage registration:

FORMS = [
    ('account', CreateUserForm),
    ('student', StudentInfoForm),
    ('parent', ParentInfoForm),
    ('teacher', TeacherInfoForm)
]

class RegisterWizard(SessionWizardView):
    form_list = FORMS
    template_name = 'authentication/register_wizard.html'

    def done(self, form_list, **kwargs):
        #Create the Account
        account_form = form_list[0]
        account = account_form.save(commit=False)
        account.save()  # Save Account instance

        # Create the Student and link to Account
        if account.roleid.role_type == 'S':
            student_form = form_list[1]
            student = student_form.save(commit=False)
            student.accountid = account  # Link the student to the created account
            student.save()  # Save Student instance
        else:
            teacher_form = form_list[2]
            teacher = teacher_form.save(commit=False)
            teacher.accountid = account  # Link the teacher to the created account
            teacher.save()  # Save Teacher instance

        # Redirect to login or any other success page
        return redirect('login') 

Which should redirect the user to a student or teacher login page depending on if they select student or teacher in the account form

it doesn’t do that however, and only goes from Account > Student > Submit form Even when I select Teacher as the role

I thought it might be related to how I have it written out in my model but i’m not sure

like this?

from django.shortcut import resolve_url
if account.roleid.role_type == 'S':
  ...
  url = resolve_url('{student login url}')
else:
  ...
    url = resolve_url('{teacher login url}')
retrun redirect(url)

Welcome @ZainZ21 !

Quoting from the docs for the done method:

This method specifies what should happen when the data for every form is submitted and validated.

This is not the location where you want to alter the flow of events through the wizard.

See the docs at Conditionally view/skip specific steps for the information on how to alter the flow.

Side note: If these four pages are creating and saving four different models, and that the flow consists only of filling out the first form and then one of the next three, I would question the need or desirability of using the form wizard here. That really isn’t the pattern for which the wizard is designed.

Hi Ken!

Thanks for the response!
You’re right that the form saves to different models. I want the form to save only student and account information if the Student is initially selected, and the same for the teacher. the models have already been created in the database, but it adds information to those specific models(Student, Teacher) from the forms.

Is there a better way of doing this than the Django FormWizard?

IMO, this is a case where I think simpler is better. Your view that handles the initial registration form can redirect you to the next form to use based upon what was selected in that registration form.

Cool. Thank you! I took your advice and made my form go to different forms depending on the role initially selected. It’s a lot simpler now and uses multiple URLs but it’s working much better

Yup, I ended up doing it this way and it worked much better than what I was trying to do, thank you!