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

问题描述

我已经创建了一个类似的插件 plugin_pool。 但是我不知道如何在此插件添加一个插件

解决方法

您的插件需要设置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,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,placeholder):
        context = super(ChildCMSPlugin,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