简体   繁体   English

如何在django cms 3.5.3中的模板上循环自定义插件数据

[英]How to loop over custom plugin data on the template in django cms 3.5.3

I am trying to implement a website that uses RSS Feeds. 我正在尝试建立一个使用RSS源的网站。 My use case is to be able to display one RSS feed at a time, then loop over to the next maybe after 5 seconds. 我的用例是一次可以显示一个RSS feed,然后可能在5秒钟后循环到下一个。 These RSS feeds need to be displayed on the same placeholder. 这些RSS feed必须显示在同一占位符上。 I am new to django cms in case you need more info please let me know. 如果您需要更多信息,我是django cms的新手,请告诉我。

I have googled a lot but all I can see is how to add plugins from the frontend and they will all automatically show inside the placeholder. 我已经在Google上搜索了很多,但是我所能看到的就是如何从前端添加插件,它们都会自动显示在占位符中。 Or modify the render method of the custom plugin class to display what you want. 或修改自定义插件类的render方法以显示所需内容。 But I want to display everything but one at a time in a continuous manner 但是我想一次连续地显示除一个以外的所有内容

@plugin_pool.register_plugin
class ExternalArticlePlugin(CMSPluginBase):
    model = ExternalArticle
    name = _("External article")
    render_template = "external_article.html"
    cache = False

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

I expect to display one RSS feed at a time inside my placeholder. 我希望在占位符中一次显示一个RSS feed。 Those feeds are links to the actual webpage with more info. 这些提要是指向实际网页的链接,其中包含更多信息。

one way would be to write a function random_rss_feed_url() in ExternalArticle Model which renders a random rss instance. 一种方法是在ExternalArticle Model中编写一个函数random_rss_feed_url() ,以渲染一个随机的rss实例。

model.py 模型

class ExternalArticle(models.Model):

    def random_rss_feed_link(self):
        return ExternalArticle.objects.order_by('?')[0].link

then you do in plugins external_article.html: 然后在插件external_article.html中进行操作:

{{ instance.random_rss_feed_link }}

Edited : 编辑

if you want to change automatically without page reload, then you need something like this in javascript in your template: 如果您想自动更改而无需重新加载页面,则您的模板中的javascript需要以下内容:

var rss_links = ['link_1', 'link_2', 'link_3']; 

setInterval(function() {

    // take random rss link
    var random_link = rss_links[Math.floor(Math.random()*rss_links.length)];

    // change the link 
    document.getElementById('rss_link_element').href = random_link;

}, 5000);   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM