Using Subdomains in Django Posted on 2009-01-05 by Dave Fowler

Django's url dispatcher doesn't handle subdomains. If you want/need to use subdomains you've got to use session middleware. If you haven't written any middleware yet don't freak out, is easy.

There's some easy documentation for extra information but for our needs all you need to do is make a Class with a function called process_request. This function gets called on any request before it is processed by the url dispatcher. It needs to return either nothing (None) or an HttpResponse object. Here's the code used to get the subdomain.

class SubdomainMiddleware:
    def process_request(self, request):
        """Parse out the subdomain from the request"""
        request.subdomain = None
        host = request.META.get('HTTP_HOST', '')
        host_s = host.replace('www.', '').split('.')
        if len(host_s) > 2:
            request.subdomain = ''.join(host_s[:-2])

Now your request object has a 'subdomain' attribute you can use in your views. Alternatively you could return an HttpResponse of any sort including redirects directly from the process_request function. Make sure to add this class to your middlware classes:

    '', )

Using subdomains on localhost can be a pain. To do so sudo edit your /etc/hosts file and add the following lines replacing with whatever you want to call your test url and the subdomains with your site's subdomains.

This locally alters the DNS for so you can use it as your localhost testing url. Unfortunately the hosts file has no * functionality to handle redirecting all subdomains so this method only works if you have a known, finite set of subdomains. If that's not the case you're going to have to use a development server or set up a proxy.

Update: I also forgot to mention that if you want your login sessions to work across all subdomains you can change the SESSION_COOKIE_DOMAIN variable as follows: