django-subdomains icon indicating copy to clipboard operation
django-subdomains copied to clipboard

remove dependency on `django.contrib.sites`

Open tkaemming opened this issue 13 years ago • 7 comments

tkaemming avatar Sep 15 '12 23:09 tkaemming

can be implemented as soon as how i decide how i want to declare the setting

tkaemming avatar Sep 16 '12 07:09 tkaemming

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.

dracos avatar Feb 28 '13 17:02 dracos

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 avatar Sep 23 '16 19:09 AndreasBackx

@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 avatar Oct 12 '17 21:10 alex-polosky

@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 avatar Oct 13 '17 12:10 AndreasBackx

@AndreasBackx awesome, works like a charm. Thanks so much! Also gives me a starting point on how to further configure it for my needs.

alex-polosky avatar Oct 13 '17 21:10 alex-polosky

Has anyone figured out how to remove the dependency on Site in the reverse method?

sushifan avatar Jun 18 '19 14:06 sushifan