在Django中,是否可以将特定域的请求路由到不同的URL方案?例如,我有域:example.com和secret.com。是否可以为不同的域名处理不同的URL方案?
example.com
secret.com
avwztpqn1#
这是一个老问题,但只是想为未来的访问者添加此信息:这可以通过一个小型的定制中间件实现,如in this article所述:
virtual_hosts = { "www.example-a.dev": "blog.urls", "www.example-b.dev": "links.urls", "www.example-c.dev": "links.urls", } class VirtualHostMiddleware: def __init__(self, get_response): self.get_response = get_response def __call__(self, request): # let's configure the root urlconf host = request.get_host() request.urlconf = virtual_hosts.get(host) # order matters! response = self.get_response(request) return response
这将使用不同的urls.py文件(即blog/urls.py或links/urls.py),这取决于HTTP_HOST报头的值。
urls.py
blog/urls.py
links/urls.py
HTTP_HOST
1条答案
按热度按时间avwztpqn1#
这是一个老问题,但只是想为未来的访问者添加此信息:这可以通过一个小型的定制中间件实现,如in this article所述:
这将使用不同的
urls.py
文件(即blog/urls.py
或links/urls.py
),这取决于HTTP_HOST
报头的值。