【问题标题】:Django CMS custom plugin load data from cms_titleDjango CMS 自定义插件从 cms_title 加载数据
【发布时间】:2016-11-17 04:37:06
【问题描述】:

我想为 Django CMS 创建一个自定义插件。正如guide 所示,我创建了一些示例。但现在的目标是创建一个从(mysql)数据库获取数据的插件。它将加载属于菜单的所有标题,因为我想要一些类似于目录的标题。

要从自定义模型中获取数据,代码如下:

  • models.py:

    from cms.models.pluginmodel import CMSPlugin
    
    from django.db import models
    
    class Hello(CMSPlugin):
    
        guest_name = models.CharField(max_length=50, default='Guest')
    
  • cms_plugins.py

从 cms.plugin_base 导入 CMSPluginBase

from cms.plugin_pool import plugin_pool

from django.utils.translation import ugettext_lazy as _
from .models import Hello

class HelloPlugin(CMSPluginBase):
    model = Hello
    name = _("Hello Plugin")
    render_template = "hello_plugin.html"
    cache = False

    def render(self, context, instance, placeholder):
        context = super(HelloPlugin, self).render(context, instance, placeholder)
        return context

plugin_pool.register_plugin(HelloPlugin)

但是由于cms_title默认属于Django-CMS,这里有哪些可能的选项?在哪里可以找到名称为 Title 的 CMS 模型的定义?将其设置为 CMSPlugin 实例会是一个坏方法吗?

【问题讨论】:

    标签: python django django-cms


    【解决方案1】:

    好吧,经过几个小时的努力,我终于成功解决了我的问题。

    首先,用CMS模型和参数标题(在db:cms_title)回答问题的一部分。使用 FK 到 CMS 标题创建新模型是正确的方法。 在 models.py 中:

    class TitlePlugin(CMSPlugin):
            title = models.ForeignKey(Title)
    

    接下来,你需要将它导入到 cms_plugins.py,所以它看起来像这样:

    from .models import TitlePlugin
    
     class MyTitlePluginClass(CMSPluginBase):
        model = TitlePlugin
        render_template = "titles.html"
        cache = False
    

    如您所见,我们加载的模型是 TitlePlugin,我们在 models.py 中定义(它具有原始 cms_title 的 FK)。

    现在,渲染它:

    def render(self, context, instance, placeholder):
    
        context = super(MyTitlePluginClass, self).render(context, instance, placeholder)
        return context
    

    但我的目标是将其加载到模板中以用作“目录”,对吗?所以我不得不改变一些事情。

    我删除了 models.py 内容(不需要!)

    新的 cms_plugins.py 也进行了修改: 第一:

    from cms.models.pagemodel import Page
    #we import the Page model
    

    和更新的班级:

    class MyTitlePluginClass(CMSPluginBase):
        model = CMSPlugin
        render_template = "titles.html"
        cache = False
    
    def render(self, context, instance, placeholder):
        pages = Page.objects.filter(in_navigation=True, publisher_is_draft=False)
    
        #context = {
        #'pages' : pages,
        #}
        # edit: append to 'pages' existing list!
        context['pages'] = pages
    
        context = super(MyTitlePluginClass, self).render(context, instance, placeholder)
        return context
    
    plugin_pool.register_plugin(MyTitlePluginClass)
    

    在模板中,我们只需使用 for 循环打印它 titles.html

    {% for page in pages %}
    <div class="panel callout ">
        {{ page }}
    </div>
    {% endfor %}
    

    【讨论】:

      猜你喜欢
      • 2012-05-26
      • 2014-04-12
      • 2019-02-10
      • 2015-09-09
      • 2020-01-26
      • 2013-04-05
      • 2012-09-24
      • 2019-08-07
      • 2013-12-16
      相关资源
      最近更新 更多