django 是否可以将特定域的请求路由到不同的URL方案?

muk1a3rh  于 12个月前  发布在  Go
关注(0)|答案(1)|浏览(125)

在Django中,是否可以将特定域的请求路由到不同的URL方案?
例如,我有域:example.comsecret.com。是否可以为不同的域名处理不同的URL方案?

avwztpqn

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.pylinks/urls.py),这取决于HTTP_HOST报头的值。

相关问题