Creating a dynamic multisite using Django
Django is a great framework for rapid web development. Because it is chock-full of features, you can create great web applications using only a couple of lines of code. One thing I found out, though, is that it doesn’t support dynamic multisites out of the box.
Dynamic multisites are dynamically created sites, which are served from a single server instance. In Django terms: one project serves multiple dynamically created sites. Well known examples of dynamic multisites are blogspot and wordpress, where users receive their own subdomain for every account.
Fortunately, Django is very easily extensible and we are going to do just that to add dynamic multisite support. We’ll be using the sites framework and a custom middleware component to enable this feature.
This tutorial is structured as follows:
- Getting started briefly explains the prerequisites for this tutorial.
- Sites framework explains how we can use the sites framework to make an application multi site aware. (screw this, just give me the code)
- Middleware explains how we can create a custom middleware which we’ll use to make Django support dynamic sites. (screw this, just give me the code)
First things first. In order to create a dynamic multisite, we need to have a site first. I’ll use the Django tutorial as a starting point. If you are new to Django I highly recommend following those steps to make yourself more familiar with the framework. Once you’ve completed the tutorial, you should be able to create polls, add answers and vote for those answers. Your directory structure should look something like this: