How to Set Up Subdomains on a Website in Django



Python


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 google.com website, a subdomain is, maps.google.com.

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

Subdomains for the domain name, example.com, might include api.example.com, maps.example.com, events.example.com, etc.

Subdomains are also commonly used for dividing a website up based on different countries or different languages. Subdomains such as es.example.com (Spanish), it.example.com (Italy), pt.example.com (Portuguese), and de.example.com (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 settings.py file of the project.

The first thing you will need to do in the settings.py 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, api.mysite.com and maps.mysite.com

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 settings.py 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 hosts.py and put it in the same directory as the settings.py 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 hosts.py.

Now we go on to create the hosts.py file.

The contents of the hosts.py file is shown below.



As stated before, the hosts.py 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: api.mysite.com and maps.mysite.com.

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 hosts.py 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 views.py function, along with a URL that maps to the appropriate page.

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



With this URL configuration, you can now go to api.mysite.com and api.mysite.com/unitedstates

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...