【问题标题】:How to limit Django-CMS template_choices based on Page parent如何根据页面父级限制 Django-CMS 模板选择
【发布时间】:2018-09-14 16:00:59
【问题描述】:

这个问题可能可以用一个更广泛的问题来解决:“如何用外部属性替换 python 2.7 属性”,但也许有一种 Django-CMS 方式来完成这个,所以我问:

我正在尝试根据其父级限制 Django-CMS' (v3.4.x) 页面的模板选择,因此为此我想用一个函数覆盖它的 template_choices,但我在Django-CMS 的 Page 模型在创建时加载,如下所示:

@python_2_unicode_compatible
class Page(...):
...
TEMPLATE_DEFAULT = get_cms_setting('TEMPLATES')[0][0]
template_choices = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]
...

修改get_cms_settins 是不可能的,但我确实需要更改TEMPLATE_DEFAULTtemplate_choices 以便它们具有我想要的正确值。由于我对 Django 和 Python 还很陌生,我的问题是 在哪里 以及 如何 我该怎么做?

【问题讨论】:

    标签: django-cms


    【解决方案1】:

    我的第一次尝试是在我的models.py 上做这样的事情:

    @property
    def template_choices(self):
        from cms.utils import get_cms_setting
        templates = [(x, _(y)) for x, y in get_cms_setting('TEMPLATES')]
        if self.parent:
            if self.parent.template == 'parent_a.html':
                templates = [('child_A.html', _('Child A'))]
            elif self.parent.template == 'parent_b.html':
                templates = [('child_b.html', _('Child B'))]
            else:
                templates = [('fullwidth.html', _('Fullwidth'))]
        else:
            templates = [('home_page.html', _('Homepage')), 
                         ('parent_a.html', _('Parent A')), 
                         ('parent_b.html', _('Parent B'))]
        return templates
    
    
    @property
    def template_default(self):
        return self.template_choices[0][0]
    
    
    Page.template_choices = template_choices
    Page.TEMPLATE_DEFAULT = template_default
    

    如果我尝试检查Page 的任何实例,这正确设置这些字段,但是当我尝试编辑任何页面并单击页面>模板菜单时,所有模板都是可供选择,所以似乎template_choicesTEMPLATE_DEFAULT 属性被忽略了。检查pagemodel.py 似乎证实了这一点,因为get_templateget_template_name 方法使用get_cms_setting('TEMPLATES') 而不是这些字段。同样在cms_toolbars.py# templates menu 部分get_cms_setting('TEMPLATES') 而不是self.page.template_choices 这似乎是罪魁祸首。所以,这个问题变成了一个bug票:https://github.com/divio/django-cms/issues/6520

    【讨论】:

      猜你喜欢
      • 2019-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-20
      • 1970-01-01
      • 2013-09-26
      相关资源
      最近更新 更多