Python Django CMS:无需ForiengKey的应用程序插件
我是CMS Django的新手,我正在尝试创建一个插件,该插件将连接到Python Django CMS:无需ForiengKey的应用程序插件,python,django,django-cms,Python,Django,Django Cms,我是CMS Django的新手,我正在尝试创建一个插件,该插件将连接到Blogapp。我想在每一页上展示5篇最新的博客文章。问题是每个插件实例都必须连接到博客应用程序中的某个实例,因为在我们的模板中,我们将使用插件的实例,比如:实例.article.all()或实例.blog.article.all() 是否有一些选项可以将文章的实例放入我的BlogPlugins模板中,而不使用BlogPlugin的实例 多谢各位 您不需要将插件连接到博客。您只需在插件的渲染方法中获取对象。render方法有点
Blog
app。我想在每一页上展示5篇最新的博客文章。问题是每个插件实例都必须连接到博客应用程序中的某个实例,因为在我们的模板中,我们将使用插件的实例,比如:实例.article.all()
或实例.blog.article.all()
是否有一些选项可以将文章的实例
放入我的BlogPlugin
s模板中,而不使用BlogPlugin
的实例
多谢各位 您不需要将插件连接到博客。您只需在插件的渲染方法中获取对象。render
方法有点像视图的get\u context\u 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
您不需要将插件连接到博客。您只需在插件的渲染方法中获取对象。render
方法有点像视图的get\u context\u 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