Hello Folks 😁 !
Welcome back to the world of learning.
In last article, we learnt about How to deploy our django project on vercel for free
In this article, I will show you how to set up subdomains on different apps inside a Django project.
Subdomains are a prefix on a URL that exists before the domain name. For example, for the csegeeks.com website, a subdomain is, keshav.csegeeks.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.
So, how do we create subdomains for a website in Django ?
It can be done in 7 Easy Steps !
It turns out there is a third-party module, django-hosts, that allows a Django website to have subdomains.
So, let's Dive IN !!
Step 1
Install django-hosts module onto your machine using
pip install django-hosts
Once this has been successfully installed, we need to make a number of changes to the settings.py
file of the project.
Step 2
Add 'django_hosts' to INSTALLED_APPS in settings.py
INSTALLED_APPS = [
'...',
'...',
'...',
'django_hosts']
Step 3
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.
ALLOWED_HOSTS = ['api.localhost', 'localhost', 'api.mysite', 'mysite']
So within this ALLOWED_HOSTS variable, we have a number of items.
Again, any subdomain that you create must be added to the ALLOWED_HOSTS variable. Or else, the subdomain will not work.
Step 4
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.
MIDDLEWARE = [
'django_hosts.middleware.HostsRequestMiddleware',
'..............................................',
'..............................................',
'..............................................',
'django_hosts.middleware.HostsResponseMiddleware']
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.
Step 5
Right underneath the ROOT_URLCONF
variable, Add the following 2 lines, shown below.
ROOT_HOSTCONF = 'mysite.hosts'
DEFAULT_HOST= 'www'
What the ROOT_HOSTCONF does is it shows the path to the URL scheme of all the subdomains on the website. This is the file that maps out all 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
.
Step 6
Now we will create hosts.py
file in Root Directory.
The contents of the hosts.py
file is shown below.
from django_hosts import patterns, host
from django.conf import settings
host_patterns = patterns('',
host(r'www', settings.ROOT_URLCONF, name='www'),
host(r'api', 'api.urls', name='api'),
)
As stated before, the hosts.py file within the project directory contains all the subdomains on the site. As you can see above, the website has a subdomain now site up: api.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.
Step 7
The last thing you must do now is create an app for api.
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.
from django.urls import path
from django.contrib import admin
from .views import (apihome, apiaddnote)
urlpatterns = [
path(r'', apihome, name='apihome'),
path(r'note', apiaddnote, name='apiaddnote'),
]
With this URL configuration, you can now go to api.mysite.com and api.mysite.com/note
If you are running this site on a local server, you can access these subdomains with, api.localhost:8000
and api.localhost:8000/note
.
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.
Finally
Testing Locally
In order to test it locally, you will need to setup a local DNS host.
On Linux and Mac, the file is located in the path /etc/hosts
. For Windows it should be somewhere in %SystemRoot%\system32\drivers\etc\hosts
.
hosts:
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
127.0.0.1 www.mysite.local
127.0.0.1 help.mysite.local
And this is how to set up subdomains for a website in Django.
So, this was another 7 Step tutorial after How to Deploy Django project to Vercel for Free (In Easy 7 Steps)
If you like my work, please consider upvoting & bookmarking the post for later you ! And don't forget to follow for more Django related Tutorial !!
Top comments (2)
I got lost at number 5 and 6.
Can you shed more light please?🙏 **
In Number 6, we create a
hosts.py
file telling the hosts pattern,And the in Number 5 I added
ROOT_HOSTCONF
and added the path of the newly createdhosts.py
file i.e,mysite.hosts
.If this makes sense now.
Otherwise let me know, I will try to further simplify it.