Improve HTMX request detection by using request.headers and falling back to META for compatibility
This commit is contained in:
+11
-3
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user