from django.http import Http404 from django.utils.deprecation import MiddlewareMixin from leftists.models import Domain class DomainAutoCreateMiddleware(MiddlewareMixin): def __init__(self, get_response): super().__init__(get_response) self.cache = set() def process_request(self, r): if (host := r.get_host()) in self.cache: return try: Domain.get_from_request(r) self.cache.add(host) except Domain.DoesNotExist: Domain.objects.create(fqdn=host.lower())