【问题标题】:How to enable per-site templates within Mezzanine multi-tenancy如何在夹层多租户中启用每站点模板
【发布时间】:2015-04-16 09:49:41
【问题描述】:

我们正在将业务扩展到欧洲,我正在使用 Mezzanine 的多租户功能在同一个 Django 安装上托管美国和欧盟版本的网站。我们在每个站点上都有一个/locations 页面,我想使用不同的模板提供服务,基于SITE_ID

我已经关注 Mezzanine 的稀疏文档 here 并将以下内容添加到 settings.py

HOST_THEMES = [
    ('domain.com', 'domain_app'), 
    ('domain.eu', 'domain_eu')
]

我在基本主题之后将domain_eu添加到INSTALLED_APPS,并使用python manage.py startapp domain_eu生成目录并手动创建了domain_eu/templates/pages/locations.html文件。

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

页面仍然使用位于 基本主题 domain_app/templates/pages/locations.html

中的位置模板呈现

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

如何根据当前SITE_ID在其对应的主题/应用目录中使用模板呈现页面?

【问题讨论】:

    标签: django mezzanine multi-tenant


    【解决方案1】:

    深入研究 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():
    

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

    我曾假设 Mezzanine 会按照 their documentation 使用会话变量 site_id,但显然不会在这个特定的模板呈现代码路径下使用。

    解决方案因此只需使用以下内容编辑我的 /etc/hosts 文件:

    127.0.0.1 domain.eu
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多