Enhance exam take functionality to support HTMX save action and partial HTML responses

This commit is contained in:
Ross
2025-11-10 21:43:19 +00:00
parent 738752c030
commit dce72f9abe
+11
View File
@@ -283,11 +283,22 @@ def exam_take(request, pk: int, sk: int, cid: str | None = None, passcode: str |
return redirect(take_url, sk=pos - 1, **kwargs)
elif "finish" in request.POST:
return redirect(finish_url, **kwargs)
elif "save" in request.POST:
# Save action should not trigger a full-page load for HTMX clients.
if is_htmx:
# Re-render the same fragment so the client can swap it in-place
return exam_take_fragment(request, pk=pk, sk=pos, cid=cid, passcode=passcode)
# Non-HTMX clients get redirected back to the same question page
return redirect(take_url, sk=pos, **kwargs)
elif "goto" in request.POST:
dest = int(request.POST.get("goto"))
if is_htmx:
return exam_take_fragment(request, pk=pk, sk=dest, cid=cid, passcode=passcode)
return redirect(take_url, sk=dest, **kwargs)
# For HTMX POSTs that don't match a navigation action (eg validation errors),
# return the fragment so the client receives only the partial HTML.
if is_htmx:
return exam_take_fragment(request, pk=pk, sk=pos, cid=cid, passcode=passcode)
else:
form = UserAnswerForm(instance=answer)