如何在插件 Django CMS 中添加插件

How to add a plugin in a plugin Django CMS

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

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

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