Refactor exam review fragment inclusion to support app-specific templates and improve HTMX handling
This commit is contained in:
@@ -6,7 +6,10 @@
|
||||
<div id="review-root">
|
||||
{# The #review-content element is replaced by HTMX when navigating between questions. #}
|
||||
<div id="review-content">
|
||||
{% include 'generic/partials/exam_review_question_fragment.html' %}
|
||||
{# The view selects an app-specific fragment if present and passes its name
|
||||
in `fragment_template`. Include that fragment here so the full-page
|
||||
render and the HTMX partials both use the same template. #}
|
||||
{% include fragment_template %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+25
-2
@@ -1216,8 +1216,31 @@ class ExamViews(View, LoginRequiredMixin):
|
||||
"can_edit": self.check_user_edit_access(request.user, exam_id=pk),
|
||||
}
|
||||
|
||||
if request.htmx:
|
||||
return render(request, "generic/partials/exam_review_question_fragment.html", context)
|
||||
# Choose an app-specific fragment if present, otherwise fall back to
|
||||
# the generic fragment. Expose the chosen fragment name in the
|
||||
# context so the full-page render can include it; return the fragment
|
||||
# directly for HTMX requests.
|
||||
from django.template import loader
|
||||
|
||||
fragment_candidates = [
|
||||
f"{self.app_name}/partials/exam_review_question_fragment.html",
|
||||
"generic/partials/exam_review_question_fragment.html",
|
||||
]
|
||||
template = loader.select_template(fragment_candidates)
|
||||
fragment_template_name = template.template.name
|
||||
context["fragment_template"] = fragment_template_name
|
||||
|
||||
# Detect HTMX: prefer the request.htmx attribute when available, otherwise
|
||||
# check the HX-Request header.
|
||||
is_htmx = getattr(request, "htmx", False)
|
||||
if not is_htmx:
|
||||
try:
|
||||
is_htmx = request.headers.get("HX-Request", "").lower() == "true"
|
||||
except Exception:
|
||||
is_htmx = request.META.get("HTTP_HX_REQUEST", "") == "true"
|
||||
|
||||
if is_htmx:
|
||||
return render(request, fragment_template_name, context)
|
||||
|
||||
return render(request, "generic/exam_review_question.html", context)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user