如何在夹层多租户中启用每个站点模板

Rob*_*lls 5 django multi-tenant mezzanine

我们正在将业务扩展到欧洲,我正在使用 Mezzanine 的多租户功能在同一个 Django 安装上托管美国和欧盟版本的网站。我们/locations在每个网站上都有一个页面,我希望根据SITE_ID.

我在这里遵循了 Mezzanine 的稀疏文档,并将以下内容添加到settings.py

HOST_THEMES = [
    ('domain.com', 'domain_app'), 
    ('domain.eu', 'domain_eu')
]
Run Code Online (Sandbox Code Playgroud)

我已添加domain_eu基本主题INSTALLED_APPS之后并用于生成目录并手动创建文件。python manage.py startapp domain_eudomain_eu/templates/pages/locations.html

然后,我复制了位置页面并将其分配给欧盟站点。

页面仍使用位于基本主题中的位置模板进行渲染 domain_app/templates/pages/locations.html

SITE_ID我已确认请求中设置了正确的内容。

如何让页面根据当前的相应主题/应用目录中的模板进行渲染SITE_ID

Rob*_*lls 4

在深入研究 Mezzanine 的代码后,我弄清楚了为什么我的 eu 主题模板没有渲染。关键的代码可以在以下位置找到mezzanine/utils/sites.py

def host_theme_path(request):
    """
    Returns the directory of the theme associated with the given host.
    """
    for (host, theme) in settings.HOST_THEMES:
        if host.lower() == request.get_host().split(":")[0].lower():
Run Code Online (Sandbox Code Playgroud)

当我记录结果时,request.get_host()我很快意识到了问题,因为它localhost显然与任何HOST_THEMES域都不匹配。

site_id我曾假设 Mezzanine 会根据其文档使用会话变量,但显然不会沿着这个特定的模板渲染代码路径。

因此,解决方案/etc/hosts就是简单地使用以下内容编辑我的文件:

127.0.0.1 domain.eu
Run Code Online (Sandbox Code Playgroud)

现在,当我访问时,domain.eu/locations它会从正确的主题目录(在本地开发环境中)渲染模板