2016-08-02 38 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>