Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Django CMS:无需ForiengKey的应用程序插件_Python_Django_Django Cms - Fatal编程技术网

Python Django CMS:无需ForiengKey的应用程序插件

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

我是CMS Django的新手,我正在尝试创建一个插件,该插件将连接到
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