Skip to content
Advertisement

One view for multiple sub-domains using django-hosts

I need to have multiple sub domains for my project. Each sub domain represents some company. For example: company1.myproject.io, company2.myproject.io. I used django-hosts library to set up sub domains.

hosts file:

127.0.0.1       localhost
127.0.0.1       myproject.io
127.0.0.1       www.myproject.io
127.0.0.1       company1.myproject.io
127.0.0.1       company2.myproject.io

settings.py:

ROOT_URLCONF = 'core.urls'
ROOT_HOSTCONF = 'core.hosts'
DEFAULT_HOST = 'www'
DEFAULT_REDIRECT_URL = "http://www.myproject.io:8000"

core/hosts.py:

from hostsconf import urls as redirect_urls
host_patterns = [
    host(r'www', settings.ROOT_URLCONF, name='www'),
    host(r'(?!www).*', redirect_urls, name='wildcard'),
]

hostsconf/urls.py:

from .views import wildcard_redirect

urlpatterns = [
    url(r'^(?P<path>.*)', wildcard_redirect)
]

hostsconf/views.py:

DEFAULT_REDIRECT_URL = getattr(settings, "DEFAULT_REDIRECT_URL", "http://www.myproject.io:8000")

def wildcard_redirect(request, path=None):
    new_url = DEFAULT_REDIRECT_URL
    if path is not None:
        new_url = DEFAULT_REDIRECT_URL + "/" + path
    return HttpResponseRedirect(new_url)

I have a few problems now:

  1. When I go to myproject.io it succesfully redirects me to the www.myproject.io. But when I go to company1.myproject.io I got an Invalid HTTP_HOST header: 'company1.myproject.io:8000'. You may need to add u'company1.myproject.io' to ALLOWED_HOSTS Do I really need each time add new host to ALLOWED_HOSTS when I got new sub domain or I am doing something wrong?
  2. How to implement a single view for all sub-domains where will be dynamic context for each company. Basically I need to make queries into the db by sub domain name. Example:

    def home_page(request):
        subdomain = 'somehow get sub domain (company1 or company2)'
        comp = User.objects.get(domain_name=subdomain)
        return redirect(request, 'tpl.html', {"company": comp})
    

UPDATE: Figured out how to handle ALLOWED_HOSTS and get a subdomain. But I still don’t get how to implement single view for my sub domains. Do I need to create another pattern in hosts.py?

Advertisement

Answer

A value beginning with a period can be used as a subdomain wildcard: ‘.example.com’ will match example.com, www.example.com, and any other subdomain of example.com. https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts

For security purposes you must add your domains to allowed_hosts list. Just use wildcard like this:

ALLOWED_HOSTS = ['.myproject.io']

2) Try HttpRequest.META[“HTTP_HOST”]

https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.META

or request.get_host()

UPDATE: If you want operate with multiple sites in single django application, you should use Django Sites framework. You don’t need django-hosts library.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement