Enhance exam take functionality to support HX-Redirect for HTMX clients, ensuring full navigation experience
This commit is contained in:
+15
-4
@@ -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 exam_take_fragment(request, pk=pk, sk=pos - 1, cid=cid, passcode=passcode)
|
||||||
return redirect(take_url, sk=pos - 1, **kwargs)
|
return redirect(take_url, sk=pos - 1, **kwargs)
|
||||||
elif "finish" in request.POST:
|
elif "finish" in request.POST:
|
||||||
# The overview is a full page; always navigate to the overview
|
# The overview is a full page. For HTMX clients, instruct the
|
||||||
# URL rather than returning an HTMX fragment. This ensures a
|
# browser to perform a full navigation by returning an HX-Redirect
|
||||||
# consistent full-page overview experience (and prevents the
|
# header (htmx will set window.location). For non-HTMX clients,
|
||||||
# overview HTML from being swapped into the question container).
|
# 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)
|
return redirect(finish_url, **kwargs)
|
||||||
elif "save" in request.POST:
|
elif "save" in request.POST:
|
||||||
# Save action should not trigger a full-page load for HTMX clients.
|
# Save action should not trigger a full-page load for HTMX clients.
|
||||||
|
|||||||
Reference in New Issue
Block a user