Submittng django formwizard data to an API
I'm needing to prevent browser from progressing to next screen if any errors occur during data submission to an API. Preferably displaying a message to the user.
In the done() I've tried to return to the last step: I have tried raising validation errors, results in validation error display on debug screen. Redirecting to the last step, resulting lost form data and last step displaying as first
I attempted submitting data using process_step() if last step, just fails horribly.
This has been very difficult to search and likely a fundamental I'm missing.
Below is the closest i can get with done()
def done(self, form_list, **kwargs):
form_data = [form.cleaned_data for form in form_list]
# form_data = process_form_data(form_list)
response: DocumentResponse = add_admission(
name_last=form_data[0]["name_last"],
name_first=form_data[0]["name_first"],
name_middle=form_data[0]["name_middle"],
date_of_birth=form_data[0]["date_of_birth"],
ssn=form_data[0]["ssn"],
sex=form_data[1]["sex"],
smoker=form_data[1]["smoker"],
veteran=form_data[1]["veteran"],
addr_street=form_data[2]["addr_street"],
addr_street2=form_data[2]["addr_street2"],
addr_city=form_data[2]["addr_city"],
addr_state=form_data[2]["addr_state"],
addr_zip=form_data[2]["addr_zip"],
phone_mobile=form_data[3]["phone_mobile"],
phone_home=form_data[3]["phone_home"],
is_disabled=form_data[1]["disabled"],
)
if response.success:
if not isinstance(response, AdmissionResponse):
raise Exception("Invalid DocumentResponse Returned.")
admission_response: AdmissionResponse = response
client_id = admission_response.client_id
triage = Screening.objects.get(name="Triage Risk")
Assignment.objects.create(client_id=client_id, screening=triage)
self.request.session["client_id"] = client_id
return HttpResponseRedirect(reverse_lazy("client-screenings"))
else:
print("We gotta redirect to the last step w/ data")
As hacky as it feels, I was able to achieve desired result by revisiting the process_steps() idea, manually adding a message and rending the last form.
def process_step(self, form):
if self.steps.current == "3":
step_form_data = form.cleaned_data
step_data = self.get_all_cleaned_data()
step_data.update(step_form_data)
print(step_data)
response: DocumentResponse = add_admission(
name_last=step_data["name_last"],
name_first=step_data["name_first"],
name_middle=step_data["name_middle"],
date_of_birth=step_data["date_of_birth"],
ssn=step_data["ssn"],
sex=step_data["sex"],
smoker=step_data["smoker"],
veteran=step_data["veteran"],
addr_street=step_data["addr_street"],
addr_street2=step_data["addr_street2"],
addr_city=step_data["addr_city"],
addr_state=step_data["addr_state"],
addr_zip=step_data["addr_zip"],
phone_mobile=step_data["phone_mobile"],
phone_home=step_data["phone_home"],
is_disabled=step_data["disabled"],
)
if response.success:
# if not isinstance(response, AdmissionResponse):
# raise Exception("Invalid DocumentResponse Returned.")
admission_response: AdmissionResponse = response
client_id = admission_response.client_id
triage = Screening.objects.get(name="Triage Risk")
Assignment.objects.create(fcc_id=client_id, screening=triage)
self.request.session["client_id"] = client_id
else:
messages.warning(self.request, response.message)
self.render_goto_step("3")
return
return super().process_step(form)