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
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.
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