Skip to content

Instantly share code, notes, and snippets.

@geoffrey-eisenbarth
Last active May 22, 2025 16:04
Show Gist options
  • Save geoffrey-eisenbarth/cc5b4c3a2d9466edef0cddbdbbce6c5e to your computer and use it in GitHub Desktop.
Save geoffrey-eisenbarth/cc5b4c3a2d9466edef0cddbdbbce6c5e to your computer and use it in GitHub Desktop.
HttpVerbViewMiddleware for Django+HTMX
from django.http import HttpRequest, HttpResponse, QueryDict
class HttpVerbViewMiddleware:
"""Adds support for all HTTP verbs.
Notes
-----
Django uses request.GET and request.POST to store QueryDicts
of the request's URL parameters and body respectively. In an
attempt to more accurately depict their roles, we store these
values in request.QUERY and request.BODY.
"""
def __init__(self, get_response) -> None:
self.get_response = get_response
def __call__(self, request: HttpRequest) -> HttpResponse:
return self.get_response(request)
def process_view(self, request, view_func, view_args, view_kwargs):
request.QUERY = request.GET.copy()
request.BODY = request.POST.copy() or QueryDict(request.body)
if not getattr(request, 'htmx', None):
if _method := (request.POST.get('_method') or request.GET.get('_method')):
request.method = _method.upper()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment