Using subdomains with Django

Django makes using beautiful urls a snap. However using subdomains with Django is not so obvious. Here is an article explaining this.

Advantages of subdomain

  1. Sometimes looks more professional. Users will prefer someblog.wordpress.com over wordpress.com/someblog.
  2. If each part of your site is Under a different untrusted users, you might want to give them a separate subdomain, so that if they do Blackhat Seo on their part, the main site is not harmed.
  3. Your users can point a Cname to their subdomain to use domain of their choice with their subdomain.

Disadvantages of subdomain

  1. When you need subdomains, you will know it. If you do not if you need it or not, you probably do not.

Getting the current subdomain is ridiculously easy.

bits = urlparse.urlsplit(request.META['HTTP_HOST'])[0].split('.')
bits[0]

now bits[0] is you subdomain.

However if you are using subdomains you are probably going to be needing this,

  1. In all your views.
  2. In all your templates.

So you need to expose the subdomains using

  1. A Middleware for all requests.
  2. A request context for all templates.

A Middleware is nothing but a normal Python class which can implement process_request, process_response and others.

The code to expose subdomains for all requests via a middleware is,

import urlparse

class GetSubdomainMiddleware:

    def process_request(self, request):
	bits = urlparse.urlsplit(request.META['HTTP_HOST'])[0].split('.')
	if not( len(bits) == 3):
	    pass#Todo Raise an exception etc
	request.subdomain = bits[0]

The way to populate subdomain in all templates is similar

def populate_board(request):
    "Populate the board in the template"
    return {'board':request.subdomain}#request.subdomain has been populated via the Middleware.

And now you need to edit you settings.py file to add TEMPLATE_CONTEXT_PROCESSORS and MIDDLEWARE_CLASSES to include your Middleware and context processor.

You are almost ready to go, however your cookies will not work across sub domains. To make your cookies work across subdomains, add this line to your settings.py

SESSION_COOKIE_DOMAIN = ‘.example.com’ if not DEBUG else ‘.local

Thanks

Thank you for reading the Agiliq blog. This article was written by shabda on Oct 10, 2008 in tips .

You can subscribe ⚛ to our blog.

We love building amazing apps for web and mobile for our clients. If you are looking for development help, contact us today ✉.

Would you like to download 10+ free Django and Python books? Get them here