Christopher Williams

Django Subdomain Middleware with CNAME Support

For a personal project (details forthcoming), I've been trying to have separate subdomains for accounts and allow some of these accounts to have CNAME support. So, if I have an account cwilliams, my data should be available at I could also add as a CNAME and get the same view.

The implementation is fairly straightforward. I ended up taking django-subdomains, removing some functionality that I didn't need and adding the CNAME handling.

To use this middleware:

  1. Put (see below) somewhere in your project. I have a Django app called core for things like this.
  2. Add a CNAME field to a model of your choosing (a user profile might be appropriate), preferably as a CharField. URLField will not work.
  3. Add a SlugField to the same model and wire that up like you would any other slug.
  4. Add a new and put the URLconf that should be used for your subdomains/CNAMEs.
  5. Add SUBDOMAIN_URLCONF='yourproject.subdomain_urls' to
  6. Add core.middleware.SubdomainMiddleware to MIDDLEWARE_CLASSES in (bottom of the list should work for most usages).
  7. Create some fake DNS entries in your hosts file (see below).
  8. Set up your slug/CNAME entries created in your model (i.e. for slugs, test1, test2. For CNAMEs,, etc.)
  9. Open your fake subdomains in your browser.

Any hostname that doesn't have a corresponding slug/CNAME will be routed through your ROOT_URLCONF. Otherwise, you should see what's in your SUBDOMAIN_URLCONF.

The Code



# Fake DNS entries for testing the subdomain middleware       test1.django.local       test2.django.local       django.local

from django.utils.cache import patch_vary_headers
from django.conf import settings
from django.db.models import Q

from account.models import Account

class SubdomainMiddleware:
    def process_request(self, request):
        # Strip off the port, mostly useful for development environments
        fqdn = request.get_host().split(':')[0]

        # Break up the domain into parts and get the subdomain slug
        domain_parts = fqdn.split('.')
        if len(domain_parts) > 2:
            subdomain = domain_parts[0]
            if subdomain.lower() == 'www':
                subdomain = None
            subdomain = None

            request.account = Account.objects.get(
                Q(cname=fqdn) |
        except Account.DoesNotExist:
            request.urlconf = settings.SUBDOMAIN_URLCONF

    def process_response(self, request, response):
        patch_vary_headers(response, ('Host',))
        return response