Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove dependency on django.contrib.sites #11

Open
tkaemming opened this issue Sep 15, 2012 · 7 comments
Open

remove dependency on django.contrib.sites #11

tkaemming opened this issue Sep 15, 2012 · 7 comments

Comments

@tkaemming
Copy link
Owner

No description provided.

@tkaemming
Copy link
Owner Author

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

@dracos
Copy link

dracos commented Feb 28, 2013

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.

@AndreasBackx
Copy link

AndreasBackx commented Sep 23, 2016

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.

@alex-polosky
Copy link

@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.

@AndreasBackx
Copy link

@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'.

@alex-polosky
Copy link

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

@sushifan
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants