Martin J. Hsu

view decorator: htmx_only

htmx is a natural extension of html. It works well with Django and other server side page generating frameworks and such. I wrote a simple decorator to require view can be accessed from htmx only.

Aside from testing, you want your htmx Django callables to be accessed from htmx only. This decorator assures this to be the case.

import functools
from django.http import HttpResponseForbidden

def htmx_only(view_func):
    @functools.wraps(view_func)
    def wrap(request, *args, **kwargs):
        if request.headers.get('HX-Request'):
            return view_func(request, *args, **kwargs)
        else:
            return HttpResponseForbidden()
    return wrap

post, django, htmx
  1. </> htmx request headers
  2.  Django, HTMX and Alpine.JS: A Match Made In Heaven
more...