1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | class SubdomainMiddleware:
""" Make the subdomain publicly available to classes """
def process_request(self, request):
domain_parts = request.get_host().split('.')
if (len(domain_parts) > 2):
subdomain = domain_parts[0]
if (subdomain.lower() == 'www'):
subdomain = None
domain = '.'.join(domain_parts[1:])
else:
subdomain = None
domain = request.get_host()
request.subdomain = subdomain
request.domain = domain
|
More like this
- django subdomain support for both resolve and reverse. by puppy 2 years, 11 months ago
- PermanentRedirectMiddleware by marinho 4 years, 9 months ago
- simple DomainsAliasMiddleware by matrix 4 years, 1 month ago
- BabelMiddleware by skam 5 years, 10 months ago
- Cookieless Session Decorator by achimnol 3 years, 9 months ago
Comments
This is useful, thanks.
However, it might be safer to use the request get_host() method instead of META['HTTP_HOST'], because it is not guaranteed to be set. E.g. in tests, META['HTTP_HOST'] is not defined.
#
Thanks. I've updated the snippet.
#
usefull. more intuitive than django-subdomains app.
#