django-subdomains
django-subdomains copied to clipboard
remove dependency on `django.contrib.sites`
can be implemented as soon as how i decide how i want to declare the setting
Future changes after 2a5ea16 have moved get_domain() into utils.py - how would I go about overriding it now? If I subclass SubdomainURLRoutingMiddleware and change get_domain_for_request, the call in utils.py's reverse() is still unchanged.
Any update on this? I've implemented my own middleware to lose the dependency, but in order to use the subdomain reverse method I still need the dependency. Would love to help out.
@AndreasBackx I know this is an old topic, but is there any chance you can detail how you were able to remove the dependency on Site? I can't seem to figure out what exactly I need to add to settings.py to get this middleware to stop throwing errors with it.
@alex-polosky I inherited from the default middleware and overrode the part where it normally calls out to django.contrib.sites.
from subdomains.middleware import SubdomainURLRoutingMiddleware
class SubdomainHostMiddleware(SubdomainURLRoutingMiddleware):
"""Django-subdomains middleware implementation with no dependency on django.contrib.sites."""
def get_domain_for_request(self, request):
# Return the host without the port
host = request.get_host().split(':')[0]
domains = host.split('.')
domain = '.'.join(domains[1:] if len(domains) > 2 else domains[:2])
return domain
domain = '.'.join(domains[1:] if len(domains) > 2 else domains[:2])
Let's say the host is 'google.com', we assume here that we always have domains: 'google' and 'com' that can never be removed. So in this case the FQDN (named domain here) that we want is 'google.com' and for 'www.google.com' we want 'google.com' as well. It will simply strip the first subdomain if there are more than 2 domains: 'a.b.c.d.com' returns 'b.c.d.com'.
@AndreasBackx awesome, works like a charm. Thanks so much! Also gives me a starting point on how to further configure it for my needs.
Has anyone figured out how to remove the dependency on Site in the reverse method?