Django

Code

Ticket #5022: middleware.2.py

File middleware.2.py, 0.8 kB (added by tzellman@gmail.com, 3 years ago)

updated -- it asumes your Site domain is just the base (e.g. mysite.com)

Line 
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         # strip out the 'www.' non web-savvy users often type 'www' in
9         # front of every website they visit.
10         domain = Site.objects.get_current().domain
11         subdomain = request.META['HTTP_HOST'].replace(domain, '').replace('.', '').replace('www', '')
12         try:
13             #you'll have to define a map of subdomains -> urlconfs in your settings
14             if subdomain in settings.DOMAINS_URLCONF:
15                 request.__setattr__('urlconf',
16                                     settings.DOMAINS_URLCONF[subdomain])
17         except: {}
18         return None