diff --git a/physics/views.py b/physics/views.py index 50e64ca0..b3a5511e 100644 --- a/physics/views.py +++ b/physics/views.py @@ -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)