How to Set Up Subdomains on a Website in Django


In this article, we show how to set up subdomains on a website in Django.

Subdomains are a prefix on a URL that exists before the domain name. For example, for the website, a subdomain is,

It's a very common thing that is used for web pages.

Subdomains for the domain name,, might include,,, etc.

Subdomains are also commonly used for dividing a website up based on different countries or different languages. Subdomains such as (Spanish), (Italy), (Portuguese), and (Germany) can be used to divide a site up based on language.

So how do we create subdomains for a website in Django?

It turns out there is a third-party module, django-hosts, that allows a Django website to have subdomains.

To install this third-party module for creating subdomains for your website, use the following line shown below.

Once this has been successfully installed, we need to make a number of changes to the file of the project.

The first thing you will need to do in the file is add 'django_hosts' to INSTALLED_APPS.

After this, we must add the new subdomains to the ALLOWED_HOSTS variable. Without adding the subdomains to the ALLOWED_HOSTS list, the subdomains won't work once you enter them into a browser.

In this example, we are going to create the subdomains, and

The ALLOWED_HOSTS variable will be equal to that as shown below.

So within this ALLOWED_HOSTS variable, we have a number of items.

The first 3 is when you are running your site locally. The last 3 is when you are running your site on a live server.

Again, any subdomain that you create must be added to the ALLOWED_HOSTS variable. Or else, the subdomain will not work.

The next thing we will need to do is add 'django_hosts.middleware.HostsRequestMiddleware' to the beginning of the MIDDLEWARE or MIDDLEWARE_CLASSES setting.

Then we will need to add 'django_hosts.middleware.HostsResponseMiddleware' to the end of the MIDDLEWARE or MIDDLEWARE_CLASSES setting.

This will look like that of the following below.

The middleware handles requests and responses.

When a user types in a URL and clicks the enter button, s/he is requesting a page. This triggers Django middleware to begin processing the request. If the domain name contains a subdomain, then the 'django_hosts.middleware.HostsRequestMiddleware' class intercepts and begins the process of retrieving this request.

After this we still have more lines of code to add to the file.

Right underneath the ROOT_URLCONF variable, we add the following 2 lines shown below.

What the ROOT_HOSTCONF does is it shows the path to the URL scheme of all the subdomains on the website. This is file that maps out all of the subdomains for the website.

Usually you want to call this file and put it in the same directory as the file. This is why in the ROOT_HOSTCONF variable, you replace 'mysite' with the name of your project. hosts exists because the file within this directory is named

Now we go on to create the file.

The contents of the file is shown below.

As stated before, the file within the project directory contains all of the subdomains on the site. As you can see above, the website has 2 different subdomains now site up: and

The www is just the generic view that doesn't return any subdomain. This means that a user will be redirected to the main site.

So we have the file done.

The last thing you must do now is create an app for api and an app for maps.

Within this app, just like any other app, you must specify a function, along with a URL that maps to the appropriate page.

So let's say we create the following file within the api app.

With this URL configuration, you can now go to and

If you are running this site on a local server, you can access these subdomains with, api.localhost:8000 and api.localhost:8000/unitedstates

You can add as many subdomains as wanted for your website. For each subdomain that you add, you have to create an app for each one. This is the correct way of doing it. This is because you are creating a subdomain to create division within your site. Therefore, you want each subdomain to have its own app.

And this is how to set up subdomains for a website in Django.

Related Resources

How to Randomly Select From or Shuffle a List in Python

HTML Comment Box is loading comments...