2016-08-02 39 views
0

我有一个简单的问题。我想知道是否可以在django-cms中定义子插件的数量限制。我的插件有子插件,但我想限制子插件的数量最多为2个。可以将它添加到cms_plugins.py的一些配置?无需手动添加表单并进行验证?可以限制django-cms中嵌套插件的数量吗?

我说这settings.py

CMS_PLACEHOLDER_CONF = { 
    'Ipp_Article_Sidebar': { 
     'plugins': ['ArticlesParentCMSPlugin', 'ArticlesChildCMSPlugin'], 
     'name': gettext("Right Side Content"), 
     'limits': { 
      'ArticlesParentCMSPlugin': 1, 
      'ArticlesChildCMSPlugin': 2 
     } 
    }, 
} 

我的占位符属于一个模型:

sidebar = PlaceholderField('ipp_article_sidebar', 
          related_name='IPP_ARTICLE_SIDEBAR') 

但我仍然可以添加超过2名儿童。

+0

见http://docs.django-cms.org/en/develop/reference/configuration.html#cms-placeholder-conf – mishbah

+0

@mishbah我更新了我的问题,你能看一下吗? :) – patricia

回答

0

目前,这不能在Django CMS中为嵌套插件完成。

我需要类似的东西,并通过覆盖模板做出快速解决方法。我加在Github上对一个类似问题的反应:https://github.com/divio/django-cms/issues/5102#issuecomment-278303995

只需添加max_children = <number>Plugin类,并在你的模板文件夹templates/cms/toolbar/创建一个新的dragitem.html模板覆盖现有的Django CMS模板。请参阅下面的比较:

当已达到最大儿童人数时,很好地禁用了+图标。

--- env/lib/python3.5/site-packages/cms/templates/cms/toolbar/dragitem.html  2016-09-15 12:06:26.132803200 +0200 
+++ templates/cms/toolbar/dragitem.html 2017-02-08 12:26:59.343312100 +0100 
@@ -9,6 +9,17 @@ 
     {% if plugin.child_plugin_instances %} cms-dragitem-collapsable{% endif %}"> 
     {% language request.toolbar.toolbar_language %} 
     {% if not disabled_child %} 
+   {% with max_children=plugin.get_plugin_instance.1.max_children child_count=plugin.child_plugin_instances|length %} 
+    {% if max_children %} 
+     <div class="cms-submenu-btn cms-submenu-add cms-btn 
+      {% if child_count >= max_children %} cms-btn-disabled{% endif %}"> 
+      {% if child_count >= max_children %} 
+       <span class="cms-hover-tooltip" data-cms-tooltip="{% trans "You cannot add plugins to this plugin." %}"></span> 
+      {% else %} 
+       <span class="cms-hover-tooltip cms-hover-tooltip-left cms-hover-tooltip-delay" data-cms-tooltip="{% trans "Add plugin" %}"></span> 
+      {% endif %} 
+     </div> 
+    {% else %} 
      <div class="cms-submenu-btn cms-submenu-add cms-btn 
       {% if not allow_children %} cms-btn-disabled{% endif %}"> 
       {% if not allow_children %} 
@@ -17,6 +28,8 @@ 
       <span class="cms-hover-tooltip cms-hover-tooltip-left cms-hover-tooltip-delay" data-cms-tooltip="{% trans "Add plugin" %}"></span> 
       {% endif %} 
      </div> 
+    {% endif %} 
+   {% endwith %} 
      <div class="cms-submenu-btn cms-submenu-edit cms-btn" data-rel="edit"> 
       <span class="cms-hover-tooltip cms-hover-tooltip-left cms-hover-tooltip-delay" data-cms-tooltip="{% trans "Edit" %}"></span> 
      </div>