https://htmx.org logo
Decorator I recently added – in case anyone needs ...
# 🔥-django-htmx
f
Decorator I recently added – in case anyone needs it. Recently realized having the login page pop up in partials isn't ideal heh. Requires django-htmx 1.6+
Copy code
python
def htmx_login_required(f):
    """ Ensure proper redirection when a HTMX request is unauthenticated – this ensures the login-form is not loaded into a partial-spot """
    def func(request, *args, **kwargs):
        if not request.htmx:  # return bad-response for non-htmx requests
            return HttpResponseBadRequest()
        if not request.user or not request.user.is_authenticated:  # redirect to login page
            return HttpResponseClientRedirect(settings.LOGIN_URL)
        return f(request, *args, **kwargs)
    return func