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:
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?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?
From a development point of view, this requires a single Django app which can run on multiple, dynamically created, domains or subdomains. It should also be able to filter content and handle permissions based on the domain from which it is accessed.
If you use a single Django app for admin panel — API back-end, user facing front-end, etc. — you probably use URL schemes like example.com/admin for the admin panel and example.com/api for the API back-end, or something similar. You should really consider serving each entity on a separate domain or subdomain. Why?
While Django can easily handle the Host header as we'll see in a moment, by default it doesn't care whether you're looking for www.example-a.dev or www.example-b.dev, it simply replies to any request on any path, regardless of the Host header. Let's see how to fix the issue.
Add django_hosts.middleware.HostsResponseMiddleware at the end of MIDDLEWARE or MIDDLEWARE_CLASSES in settings.py. Create a file hosts.py in your app. Leave it blank for now, we’ll add content in it later. Add ROOT_HOSTCONF setting in settings.py to declare path to hosts.py file you just created in previous step. eg. ROOT_HOSTCONF = 'myapp.hosts'
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With