..
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 | from django.conf import settings
from django.shortcuts import redirect
class SubdomainMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
request.subdomain = None
host = request.get_host()
host = host.replace('www.', '').split('.')
if len(host) > 2 and '127.0.0.1' not in request.get_host():
request.subdomain = '.'.join(host[:-2])
if len(host) == 2 and 'localhost' in request.get_host():
request.subdomain = host[0]
# check if ip address
if len(host) == 4 and all([x.isdigit() for x in host]):
# redirect to thatcomputerscientist.com
is_ssl = request.is_secure()
protocol = 'https' if is_ssl else 'http'
return redirect(f'{protocol}://thatcomputerscientist.com')
return self.get_response(request)
# Use different urlpatterns for each subdomain
class SubdomainURLRouting:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
configured_subdomains = getattr(settings, 'CONFIGURED_SUBDOMAINS', {})
if request.subdomain:
if request.subdomain in configured_subdomains:
request.urlconf = configured_subdomains[request.subdomain] + '.urls'
else:
if '*' in configured_subdomains:
request.urlconf = configured_subdomains['*'] + '.urls'
else:
root_urlconf = getattr(settings, 'ROOT_URLCONF', None)
request.urlconf = root_urlconf
return self.get_response(request)
|
|