Using formset in django formtool
am creating a 8 steps form wizard. 2 of the models are Qualification and SSCE and i used formset to create multiple forms inputs
Formset factory
QualificationsFormSet = modelformset_factory(Qualifications, form=QualificationsForm, extra=5) SscesFormSet = modelformset_factory(Ssces, form=SSCESubjectForm, extra=5)
the View
class StudentWizard(LoginRequiredMixin, SessionWizardView):
form_list = FORMS
# template_name = 'admission/form_wizard.html'
file_storage = FileSystemStorage(location='/tmp/')
def get_form(self, step=None, data=None, files=None):
if step is None:
step = self.steps.current
form = super().get_form(step, data, files)
form.request = self.request
if step == '3':
form = QualificationsFormSet(data, queryset=Qualifications.objects.none())
elif step == '4':
form = SscesFormSet(data, queryset=Ssces.objects.none())
return form
def get_form_instance(self, step):
print(f"Current step: {step}")
if step == '4': # SSCE step
qualifications = self.get_cleaned_data_for_step('3')
print(f"Qualifications data: {qualifications}")
if qualifications:
certificates = [qual['certificate_obtained'] for qual in qualifications]
print(f"Certificates: {certificates}")
if not any(cert in ['WASC', 'SSCE', 'NECO', 'GCE'] for cert in certificates):
print("Skipping SSCE step")
return None # Skip the Ssces formset
return super().get_form_instance(step)
def get_context_data(self, form, **kwargs):
context = super().get_context_data(form=form, **kwargs)
if self.steps.current in ["3", "4"]:
context['formset'] = form
context['formset_with_widgets'] = [
[(field, field.field.widget.__class__.__name__) for field in form]
for form in context['formset'].forms
]
else:
context['fields_with_widgets'] = [
(field, field.field.widget.__class__.__name__) for field in form
]
return context
the problem is that the other forms works well upto the Qualification where after submiting the formset for the qualification, the wizard still return to the qualification template instead of progressing to the ssce.html template.
output of the first time formset enter image description here
secondly it return empty form of the qualification template after the first submission enter image description here
i have used debug to check where the error would have been. i noticed it's arround get_form() and get_form_instance()
the core seems to be alright. i don't know where i mess it up
please, i need help to fixed this. thank you all inanticipation