Skip to content

Commit b644abb

Browse files
committed
Add get_request_handler so htmx can re-use response logic
1 parent a639fb5 commit b644abb

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

bolt-htmx/bolt/htmx/views.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,18 @@ def get_template_response(self, context=None) -> HttpResponse:
2222

2323
return super().get_template_response(context=context)
2424

25-
def get_response(self):
25+
def get_request_handler(self):
2626
if self.is_htmx_request:
2727
# You can use an htmx_{method} method on views
2828
# (or htmx_{method}_{action} for specific actions)
2929
method = f"htmx_{self.request.method.lower()}"
3030
if self.htmx_action_name:
3131
method += f"_{self.htmx_action_name}"
3232

33-
handler = getattr(self, method, None)
34-
if handler:
35-
return handler()
33+
if handler := getattr(self, method, None):
34+
return handler
3635

37-
return super().get_response()
36+
return super().get_request_handler()
3837

3938
def get_template_names(self):
4039
# TODO is this part necessary anymore?? can I replace those with fragments now?

bolt/views/base.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ def view(request, *args, **kwargs):
5353

5454
return view
5555

56-
def get_response(self) -> HttpResponseBase:
56+
def get_request_handler(self) -> callable:
57+
"""Return the handler for the current request method."""
5758
if not self.request.method:
5859
raise AttributeError("HTTP method is not set")
5960

@@ -63,6 +64,12 @@ def get_response(self) -> HttpResponseBase:
6364
handler = getattr(
6465
self, self.request.method.lower(), self._http_method_not_allowed
6566
)
67+
68+
return handler
69+
70+
def get_response(self) -> HttpResponseBase:
71+
handler = self.get_request_handler()
72+
6673
try:
6774
result = handler()
6875
except HttpResponseException as e:

0 commit comments

Comments
 (0)