Refactor HTMX response handling in ExamViews: streamline rendering of updated CID user list items and ensure consistent response structure
This commit is contained in:
@@ -8,7 +8,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="btn-group ms-2">
|
<div class="btn-group ms-2">
|
||||||
<a href="{% url 'generic:update_cid' cid.pk %}" class="btn btn-sm btn-outline-light">Edit</a>
|
<a href="{% url 'generic:update_cid' cid.pk %}" class="btn btn-sm btn-outline-light">Edit</a>
|
||||||
<form method="post" class="d-inline m-0 p-0"
|
<form method="post" class="d-inline m-0 p-0"
|
||||||
action="{% url exam.app_name|add:':exam_json_edit' exam.pk %}"
|
action="{% url exam.app_name|add:':exam_json_edit' exam.pk %}"
|
||||||
hx-post="{% url exam.app_name|add:':exam_json_edit' exam.pk %}"
|
hx-post="{% url exam.app_name|add:':exam_json_edit' exam.pk %}"
|
||||||
hx-target="closest li"
|
hx-target="closest li"
|
||||||
|
|||||||
+11
-14
@@ -2379,20 +2379,17 @@ class ExamViews(View, LoginRequiredMixin):
|
|||||||
else:
|
else:
|
||||||
app_exam_map[self.app_name].remove(pk)
|
app_exam_map[self.app_name].remove(pk)
|
||||||
|
|
||||||
# If this is an HTMX request, return the rendered li partial
|
# Always return the rendered list-item HTML so HTMX (or our fetch fallback)
|
||||||
if request.headers.get('HX-Request') == 'true':
|
# can swap the updated `<li>` into the DOM. Also provide an `HX-Trigger`
|
||||||
# Render the updated list item and return as HTML so HTMX can swap it
|
# header so client-side toasts/handlers can respond.
|
||||||
html = render_to_string(
|
html = render_to_string(
|
||||||
'generic/partials/cid_user_li.html',
|
'generic/partials/cid_user_li.html',
|
||||||
{'cid': cid_user, 'exam': exam, 'current': add},
|
{'cid': cid_user, 'exam': exam, 'current': add},
|
||||||
request=request,
|
request=request,
|
||||||
)
|
)
|
||||||
response = HttpResponse(html, content_type='text/html')
|
response = HttpResponse(html, content_type='text/html')
|
||||||
response['HX-Trigger'] = json.dumps({'cid_toggled': {'pk': cid_user.pk, 'added': add}})
|
response['HX-Trigger'] = json.dumps({'cid_toggled': {'pk': cid_user.pk, 'added': add}})
|
||||||
return response
|
return response
|
||||||
|
|
||||||
data = {"status": "success", "added": add}
|
|
||||||
return JsonResponse(data, status=200)
|
|
||||||
|
|
||||||
if "edit_user_user" in request.POST:
|
if "edit_user_user" in request.POST:
|
||||||
user_id = request.POST.get("edit_user_user")
|
user_id = request.POST.get("edit_user_user")
|
||||||
|
|||||||
Reference in New Issue
Block a user