From 200a6e2a1cc82460e2a33b3cb2a102e8f0df90fa Mon Sep 17 00:00:00 2001 From: Ross Date: Mon, 10 Nov 2025 21:58:12 +0000 Subject: [PATCH] Enhance exam take functionality to support HX-Redirect for HTMX clients, ensuring full navigation experience --- physics/views.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/physics/views.py b/physics/views.py index 43229fd9..e705a6fd 100644 --- a/physics/views.py +++ b/physics/views.py @@ -282,10 +282,21 @@ def exam_take(request, pk: int, sk: int, cid: str | None = None, passcode: str | return exam_take_fragment(request, pk=pk, sk=pos - 1, cid=cid, passcode=passcode) return redirect(take_url, sk=pos - 1, **kwargs) elif "finish" in request.POST: - # The overview is a full page; always navigate to the overview - # URL rather than returning an HTMX fragment. This ensures a - # consistent full-page overview experience (and prevents the - # overview HTML from being swapped into the question container). + # The overview is a full page. For HTMX clients, instruct the + # browser to perform a full navigation by returning an HX-Redirect + # header (htmx will set window.location). For non-HTMX clients, + # perform a normal redirect. + if is_htmx: + try: + url = reverse(finish_url, kwargs=kwargs) + except Exception: + # Fallback: build a simple redirect response + resp = HttpResponse() + resp["HX-Redirect"] = "" + return resp + resp = HttpResponse() + resp["HX-Redirect"] = url + return resp return redirect(finish_url, **kwargs) elif "save" in request.POST: # Save action should not trigger a full-page load for HTMX clients.