【问题标题】:How to add a plugin in a plugin Django CMS如何在插件 Django CMS 中添加插件
【发布时间】:2021-01-11 19:20:27
【问题描述】:

我已经创建了一个插件 plugin_pool。 但是我不知道如何在这个插件中添加另一个插件。

【问题讨论】:

    标签: python django content-management-system


    【解决方案1】:

    您的插件需要设置allow_children 属性,然后您可以通过在child_classes 列表中指定插件类来控制插件可以拥有的子级,如下所示。

    from .models import ParentPlugin, ChildPlugin
    
    @plugin_pool.register_plugin
    class ParentCMSPlugin(CMSPluginBase):
        render_template = 'parent.html'
        name = 'Parent'
        model = ParentPlugin
        allow_children = True  # This enables the parent plugin to accept child plugins
        # You can also specify a list of plugins that are accepted as children,
        # or leave it away completely to accept all
        # child_classes = ['ChildCMSPlugin']
    
        def render(self, context, instance, placeholder):
            context = super(ParentCMSPlugin, self).render(context, instance, placeholder)
            return context
    
    
    @plugin_pool.register_plugin
    class ChildCMSPlugin(CMSPluginBase):
        render_template = 'child.html'
        name = 'Child'
        model = ChildPlugin
        require_parent = True  # Is it required that this plugin is a child of another plugin?
        # You can also specify a list of plugins that are accepted as parents,
        # or leave it away completely to accept all
        # parent_classes = ['ParentCMSPlugin']
    
        def render(self, context, instance, placeholder):
            context = super(ChildCMSPlugin, self).render(context, instance, placeholder)
            return context
    

    然后,您的父插件模板需要在这样的地方渲染子插件;

    {% load cms_tags %}
    
    <div class="plugin parent">
        {% for plugin in instance.child_plugin_instances %}
            {% render_plugin plugin %}
        {% endfor %}
    </div>
    

    它的文档在您链接到的那个页面上;

    http://docs.django-cms.org/en/latest/how_to/custom_plugins.html#nested-plugins

    【讨论】:

      猜你喜欢
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 2014-09-21
      • 2020-01-26
      • 2011-06-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多