Improve HTMX request detection by using request.headers and falling back to META for compatibility

This commit is contained in:
Ross
2025-11-08 22:15:03 +00:00
parent 8ad47ffc91
commit 133a63cecc
+11 -3
View File
@@ -1217,9 +1217,17 @@ class ExamViews(View, LoginRequiredMixin):
}
# If this is an HTMX request, return only the fragment so the client
# can swap it into the page. HTMX sends the HX-Request header which
# is available in Django as HTTP_HX_REQUEST in request.META.
if request.META.get("HTTP_HX_REQUEST") == "true":
# can swap it into the page. HTMX sets the `HX-Request` header; use
# request.headers (case-insensitive) where available and fall back to
# the META variant for older environments/proxies.
is_htmx = False
try:
is_htmx = request.headers.get("HX-Request", "").lower() == "true"
except Exception:
# Fall back to META if request.headers isn't available (older Django)
is_htmx = request.META.get("HTTP_HX_REQUEST", "") == "true"
if is_htmx:
return render(request, "generic/partials/exam_review_question_fragment.html", context)
return render(request, "generic/exam_review_question.html", context)