[英]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.例如:company1.myproject.io,company2.myproject.io。 I used django-hosts library to set up sub domains.我使用django-hosts库来设置子域。
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:设置.py:
ROOT_URLCONF = 'core.urls'
ROOT_HOSTCONF = 'core.hosts'
DEFAULT_HOST = 'www'
DEFAULT_REDIRECT_URL = "http://www.myproject.io:8000"
core/hosts.py:核心/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:主机配置文件/urls.py:
from .views import wildcard_redirect
urlpatterns = [
url(r'^(?P<path>.*)', wildcard_redirect)
]
hostsconf/views.py:主机配置文件/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
但是当我 go 到 company1.myproject.io 时,我得到了一个Invalid HTTP_HOST header: 'company1.myproject.io:8000'. You may need to add u'company1.myproject.io' to ALLOWED_HOSTS
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? Invalid HTTP_HOST header: 'company1.myproject.io:8000'. You may need to add u'company1.myproject.io' to ALLOWED_HOSTS
添加到 ALLOWED_HOSTS 当我获得新的子域或我做错了什么时,我真的需要每次都将新主机添加到 ALLOWED_HOSTS 吗?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.更新:弄清楚如何处理 ALLOWED_HOSTS 并获得子域。 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?我需要在 hosts.py 中创建另一个模式吗?
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.以句点开头的值可以用作子域通配符:“.example.com”将匹配 example.com、www.example.com 和 example.com 的任何其他子域。 https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts https://docs.djangoproject.com/en/1.11/ref/settings/#allowed-hosts
For security purposes you must add your domains to allowed_hosts list.出于安全目的,您必须将您的域添加到 allowed_hosts 列表中。 Just use wildcard like this:只需像这样使用通配符:
ALLOWED_HOSTS = ['.myproject.io']
2) Try HttpRequest.META["HTTP_HOST"] 2) 尝试 HttpRequest.META["HTTP_HOST"]
https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.META https://docs.djangoproject.com/en/1.11/ref/request-response/#django.http.HttpRequest.META
or request.get_host()或 request.get_host()
UPDATE: If you want operate with multiple sites in single django application, you should use Django Sites framework .更新:如果你想在单个 django 应用程序中操作多个站点,你应该使用Django Sites 框架。 You don't need django-hosts library.你不需要 django-hosts 库。
This question seems to be vert old but I think it will be helpfull for anyone who is still searching for this.这个问题似乎很老,但我认为这对仍在寻找这个问题的人会有帮助。
For wildcard subdomain, adding the domain in a list does not work for me.对于通配符子域,在列表中添加域对我不起作用。 I have to add it as a tuple.我必须将它添加为一个元组。
ALLOWED_HOSTS = ['.domain.com'] - This does not work for me
ALLOWED_HOSTS = ('.domain.com',) - This works for me.
I tested it on Django 2.2.5 and Django 3.6我在 Django 2.2.5 和 Django 3.6 上测试过
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.