1 | from django.conf import settings
|
---|
2 | from django.contrib.sites.models import Site
|
---|
3 |
|
---|
4 | class SubdomainURLsMiddleware:
|
---|
5 | """ change the URL conf based on the subdomain in the request """
|
---|
6 |
|
---|
7 | def process_request(self, request):
|
---|
8 | # Split the domain into it's parts, remove the main part of the domain
|
---|
9 | # from the requested host, and we're left with one variable: 'subdomain'.
|
---|
10 | # We also strip out the 'www.' non web-savvy users often type 'www' in
|
---|
11 | # front of every website they visit.
|
---|
12 | django_site = Site.objects.get_current()
|
---|
13 | domain_parts = django_site.domain.split(".")
|
---|
14 | domain = ".".join(domain_parts[1:])
|
---|
15 | subdomain = request.META['HTTP_HOST'].replace(domain, '').replace('.', '').replace('www', '')
|
---|
16 | try:
|
---|
17 | #you'll have to define a map of subdomains -> urlconfs in your settings
|
---|
18 | if subdomain in settings.DOMAINS_URLCONF:
|
---|
19 | request.__setattr__('urlconf',
|
---|
20 | settings.DOMAINS_URLCONF[subdomain])
|
---|
21 | except: {}
|
---|
22 | return None
|
---|