About Requiring Login for HTMX Views
Don't use @login_required
decorator for HTMX views. Instead use a response with HX-Redirect
header, e.g.:
from django.shortcuts import render, resolve_url
from django.conf import settings
from django_htmx.http import HttpResponseClientRedirect
def htmx_personal_info(request):
if not request.user.is_authenticated:
login_url = resolve_url(settings.LOGIN_URL)
return HttpResponseClientRedirect(login_url)
return render(request, "core/htmx/personal_info.html")
HttpResponseClientRedirect
from django-htmx
creates a HttpResponse
with HX-Redirect
header and redirects the visitor to the login page on the client side, instead of rendering a login page inside the widget from which it has been requested.
Tips and Tricks Development Django 5.2 Django 4.2 Django 3.2 htmx django-htmx
Also by me
Django Paddle Subscriptions app
For Django-based SaaS projects.
Django GDPR Cookie Consent app
For Django websites that use cookies.