Skip to content

Commit b54a7ff

Browse files
feat: add dispatch method to HxRequest as a per-handler entry point
Route the HTMX path through the resolved HxRequest's own dispatch() instead of grabbing get/post off the class directly. dispatch() mirrors Django's View.dispatch and gives every handler one overridable seam for per-handler logic (auth, setup, logging) before get/post runs. Default behavior is unchanged. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent c993575 commit b54a7ff

2 files changed

Lines changed: 21 additions & 7 deletions

File tree

hx_requests/hx_requests.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.contrib.messages import get_messages
99
from django.core.exceptions import ObjectDoesNotExist
1010
from django.forms import Form
11-
from django.http import HttpRequest, HttpResponse
11+
from django.http import HttpRequest, HttpResponse, HttpResponseNotAllowed
1212
from django.template import RequestContext
1313
from django.template.loader import render_to_string
1414
from django.utils.functional import cached_property
@@ -313,6 +313,21 @@ def _get_response(self, **kwargs):
313313
headers=self.get_headers(**kwargs),
314314
)
315315

316+
def dispatch(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
317+
"""
318+
Entry point for a resolved HxRequest, mirroring Django's
319+
``View.dispatch``.
320+
321+
Routes the request to ``get`` or ``post`` based on the request
322+
method. Override this to run per-handler logic (authorization,
323+
setup, logging) before the method handler runs, calling
324+
``super().dispatch(...)`` to continue routing.
325+
"""
326+
handler = getattr(self, request.method.lower(), None)
327+
if handler is None:
328+
return HttpResponseNotAllowed(["GET", "POST"])
329+
return handler(request, *args, **kwargs)
330+
316331
def get(self, request: HttpRequest, *args, **kwargs) -> HttpResponse:
317332
"""
318333
Method that all GET requests hit.

hx_requests/views.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,15 @@ def dispatch(self, request, *args, **kwargs):
3232
# defer to the error handler. Also defer to the error handler if the
3333
# request method isn't on the approved list.
3434
if request.method.lower() in self.http_method_names:
35-
handler_class = self
36-
# If it's an HTMX request, use the HxRequest class to handle the request
37-
# otherwise, use the view class to handle the request.
35+
# If it's an HTMX request, hand off to the resolved HxRequest's
36+
# own dispatch; otherwise, let the view class handle the request.
3837
if is_htmx_request(request):
3938
kwargs.update(self.get_hx_extra_kwargs(request))
40-
handler_class = self._setup_hx_request(request, *args, **kwargs)
39+
hx_request = self._setup_hx_request(request, *args, **kwargs)
4140

4241
# Use request from hx_request, in case use_current_url is set to True
43-
request = handler_class.request
44-
handler = getattr(handler_class, request.method.lower(), self.http_method_not_allowed)
42+
return hx_request.dispatch(hx_request.request, *args, **kwargs)
43+
handler = getattr(self, request.method.lower(), self.http_method_not_allowed)
4544
else:
4645
handler = self.http_method_not_allowed
4746
return handler(request, *args, **kwargs)

0 commit comments

Comments
 (0)