From 133a63ceccc9b75c4d97d10912dbd7f0725cc079 Mon Sep 17 00:00:00 2001 From: Ross Date: Sat, 8 Nov 2025 22:15:03 +0000 Subject: [PATCH] Improve HTMX request detection by using request.headers and falling back to META for compatibility --- generic/views.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/generic/views.py b/generic/views.py index a09a86bf..f516ce48 100644 --- a/generic/views.py +++ b/generic/views.py @@ -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)