【问题标题】:Django CMS: Plugin to app without ForiengKeyDjango CMS:没有 ForiengKey 的应用程序插件
【发布时间】:2018-12-03 14:54:40
【问题描述】:

我是 CMS Django 的新手,我正在尝试创建一个将连接到 Blog 应用程序的插件。我想在每一页上显示 5 篇最新的博客文章。问题是每个插件实例都必须连接到博客应用程序中的某个实例,因为在我们的模板中,我们将使用插件的instance,例如:instance.article.all()instance.blog.article.all()

是否可以在不使用instanceBlogPlugin 的情况下将Article 的实例放入我的BlogPlugins 模板的模板中?

谢谢。

【问题讨论】:

    标签: python django django-cms


    【解决方案1】:

    您不需要将插件连接到博客。您可以在插件的渲染方法中获取对象。 render 方法有点像视图的get_context_data。例如,您可以在该方法中添加插件所需的内容;

    class BlogPlugin(CMSPluginBase):
        ...
    
        def render(self, context, instance, placeholder):
            context = super(MyPlugin, self).render(context, instance, placeholder)
    
            # If you know that the higher the `id`, the newer the object, 
            # this gets the latest 5 by ID in reverse order
            context['articles'] = Article.objects.all().order_by('-id')[:5]
    
            return context
    

    【讨论】:

      猜你喜欢
      • 2018-09-19
      • 2011-10-09
      • 2016-08-04
      • 2011-07-07
      • 1970-01-01
      • 1970-01-01
      • 2013-02-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多