How to Create Infinite Domains Using Django Hosts

July 16, 2020, 10:28 a.m.

How to Create Infinite Domains Using Django Hosts

Subdomains are used for a variety of reasons including but not limited to eCommerce stores, location or language-specific sites, and version testing.

For example, if you own the domain website.com the blog can be on the subdomain blog.website.com and the eCommerce store can be at store.website.com.

However, it is important to keep in mind search engines such as Google see subdomains as separate sites independent of the main domain.

If you are trying to grow your site via browser search rankings, we recommend using subdirectories. Subdirectories, such as website.com/blog, are arguably better for SEO given that all pages created under the subdirectory are considered part of the main site.

But if you are interested in sending users directly to your eCommerce site via ads or recreating your site for a Spanish-speaking audience, then a subdomain may be the better option.

It all depends on the intention and purpose of the site.

 


 

In this article, we are going to go over the configurations required to get a subdomain up and running on your Django project. With the Django project mysite, we will create two apps, one called main and the other blog. The blog app will connect to blog.website.com while the main app will appear when the website.com is requested.

 

Create a Django project and the main app

macOS Terminal

(env)User-Macbook:env user$ pip install django==2.1.15

(env)User-Macbook:env user$ django-admin startproject mysite

(env)User-Macbook:env user$ cd mysite

(env)User-Macbook:mysite user$ python3 manage.py startapp main

Windows Command Prompt

(env)C:\Users\Owner\desktop\env> pip install django==2.1.15

(env)C:\Users\Owner\desktop\env> django-admin startproject mysite

(env)C:\Users\Owner\desktop\env> cd mysite

(env)C:\Users\Owner\desktop\env\mysite> py manage.py startapp main

Once your virtual environment is created and activated, install django, create the project mysite, then create the app main. For simplicity, the Django project will always be referred to as mysite. If you already have an app, then skip down to creating a blog app of the article.

If you are new to Django, refer to this beginner's guide. Otherwise, configure your app's settings and add HTML templates, URLs, and views needed.

Then, when you are ready, we can move on to the blog app. 

 

Create a new app called blog

macOS Terminal

(env)User-Macbook:mysite user$ python3 manage.py startapp blog

Windows Command Prompt

(env)C:\Users\Owner\desktop\env\mysite> py manage.py startapp blog

With the main app configured, return to your CLI and create a new app called blog. This is the app that will connect to the subdomain blog.website.com

 

Create the blog URLs

from django.urls import path
from . import views

app_name = "blog"   


urlpatterns = [
    path("", views.homepage, name="homepage"),
]

Similar to the configurations made to main, you will need to add a URL file to the app blog.

From there, you will need to add the app to the list of installed apps in the settings, create the templates, and the views that connect to your URL paths. If you need to learn more about setting up your templates, views and URLs see Django configurations.

 

Install the django-hosts package

macOS Terminal

(env)User-Macbook:mysite user$ pip install django-hosts

Windows Command Prompt

(env)C:\Users\Owner\desktop\env\mysite> pip install django-hosts

Once your two apps are configured, you can use django-hosts. Created by jazzband, django-hosts is an app designed to route requests to different URL schemes defined in the hosts.py file.

 

Add the app to the installed apps in the settings

env > mysite > mysite > settings.py

INSTALLED_APPS = [
    'main.apps.MainConfig',
    'blog.apps.BlogConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django_hosts',             
]

Go to the settings.py file and add django_hosts to the list of install apps. As you can see, the apps main and blog are also listed among the installed apps.

 

env > mysite > mysite > settings.py

MIDDLEWARE = [
    'django_hosts.middleware.HostsRequestMiddleware',
    ...
    'django_hosts.middleware.HostsResponseMiddleware',
]

Then find the middleware listed below the installed apps and add the HostsRequestMiddleware to the top of the list and the HostsResponseMiddleware to the end of the list.

 

Create the hosts file

env > mysite > mysite > (New File) hosts.py

from django_hosts import patterns, host

host_patterns = patterns(
	'',
    host(r'', 'mysite.urls', name=' '),
    host(r'blog', 'blog.urls', name='blog'),
)

Now create the Python file hosts.py in the same folder as settings.py. Within this file, import patterns and host form django_hosts then specify the host_patterns. The host patterns are the subdomains. The empty host pattern are all the domains in the mysite.urls while the blog pattern is the subdomain blog.

 

Add the app to the installed apps in the settings

env > mysite > mysite > settings.py

ROOT_HOSTCONF = 'mysite.hosts'

DEFAULT_HOST = ' '

At this point, we need to add the rest of the configurations. Add the ROOT_HOSTCONF, setting it equal to the hosts.py path. Then add the DEFAULT_HOST=' '. The default host is the name of the host pattern that is called if no other pattern matches a request. 

 

env > mysite > mysite > settings.py

ALLOWED_HOSTS = [
    '127.0.0.1',
    ...
    'blog.localhost',
    ]

The last thing we need to do is add the host blog.localhost to the list of allowed hosts in the settings. If you forget to add this host, you will not have access to the blog in the local browser even after adding a local DNS host for the subdomain.

 

Access the subdomain on your localhost

If you run the server and visit 127.0.0.1:800, the main app is displaying.

But to get to the blog app, we need to visit blog.local:8000. If you try to access this page, it does not load. That's because there is no local DNS host that matches. We need to add it to the list of hosts. 

 

For Windows:

Click on the Windows icon in the bottom left corner of your screen. Then search for the app Notepad.

Before opening the app click the down arrow under "Open", and select "Run as administrator".

Once Notepad is open, click File > Open and navigate to This PC/Local Disk/Windows/System32/drivers and select the folder "etc".

If no files are showing in the "etc" folder, you need to change "Text Documents" to "All Files" in the drop-down menu.

Then you can select and open the file called "hosts".

 

This PC > Local Disk > Windows > System32 > drivers > etc > hosts

# localhost name resolution is handled within DNS itself.
#	127.0.0.1       localhost
#	::1             localhost
#	127.0.0.1       blog.localhost

Find where the localhost 127.0.0.1 is listed, then add a new localhost called blog.localhost. Be sure to save the file. 

 

For macOS:

For mac users, the process is similar.

System Preferences > Network (Choose your network connection) > Advanced > DNS

127.0.0.1 localhost
255.255.255.255 broadcasthost
::1             localhost

127.0.0.1 blog.localhost

With the DNS records updated, you can then access the subdomain for your browser. If you are running into issues, review the steps above or switch to a different browser.


0
Subscribe now

Subscribe to stay current on our latest articles and promos





Post a Comment
Join the community

0 Comments