如何从Django模板中的页面遍历cms插件实例? [英] How to iterate over cms plugin instances from a page in a Django template?

查看:389
本文介绍了如何从Django模板中的页面遍历cms插件实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想显示一个YouTube视频列表,让我的用户编辑此列表。

I want to display a list of YouTube videos and let my user edit this list.

我以为这样做:


  1. 制作一个模板,显示这个模板。在此模板中,对<%c $ c>< ul> {%for page in page%}< li> plugin< / li> {%endfor%}< / ul>

  2. 设置 youtube_videos 占位符,并将其配置为仅限于该类型的插件。

  1. Make a template where this will be displayed. In this template do something like <ul>{% for plugin in page %}<li>plugin</li>{% endfor %}</ul>.
  2. Make a youtube_videos placeholder and configure it to be limited to only that type of plugin.

但是我不知道如何对模板中当前页面中的插件实例进行此迭代。我在django-cms文档中没有看到任何内容,是的,我猜django-cms只是django,如果我已经知道django正确,那么我已经知道了这一点。

But I don't know how to make this iteration over the plugin instances in the current page in the template. I didn't see anything about this in django-cms documentation and, yes, I guess that django-cms "is just django" and if I'd had known django proper then I would have figured this out already.

但是这里的一个很好的例子是很好的。

But a nice example here would be nice.

推荐答案

在Django-CMS中的插件实例。占位符以线性方式简单地渲染分配给它们的插件。插件可以在占位符中进行拖放以重新安排,但据我所知,您不能在模板级别迭代插件,至少不容易。

You don't iterate over plugin instances in Django-CMS. Placeholders simply render the plugins that are assigned to them, in linear fashion. Plugins can be dragged-and-dropped within placeholders to re-arrange them, but to the best of my knowledge, you can't iterate over the plugins themselves at the template level, at least not easily.

为了做你想要的,你需要创建一个CMS插件,允许你创建一个可以迭代的模型的多个实例,类似于图像库。

To do what you want, you would need to create a CMS plugin that allows you to create multiple instances of a model that you could iterate over, similar to an "image gallery".

在概念上,您将有一个父模型:

Conceptually, you would have a parent model:

class Gallery(CMSPlugin):
    """ A model that serves as a container for images """

    title = models.CharField(max_length=50, help_text='For reference only')

    def copy_relations(self, oldinstance):
        for slide in oldinstance.slides.all():
            slide.pk = None
            slide.gallery = self
            slide.save()

    def __unicode__(self):
        return self.title

和子模型:

class Slide(models.Model):
    def get_upload_to(instance, filename):
        return 'galleries/{slug}/{filename}'.format(
            slug=slugify(instance.gallery.title), filename=filename)

    title = models.CharField(max_length=100)
    image = models.ImageField(upload_to=get_upload_to)
    alt = models.CharField(max_length=100)
    gallery = SortableForeignKey(Gallery, related_name='slides')

    def __unicode__(self):
        return self.title

那么你会有一个这样的CMS插件: p>

then you'd have a CMS Plugin like so:

class CMSGalleryPlugin(CMSPluginBase):
    admin_preview = False
    inlines = Slide
    model = Gallery
    name = _('Gallery')
    render_template = 'gallery/gallery.html'

    def render(self, context, instance, placeholder):
        context.update({
            'gallery': instance,
            'placeholder': placeholder
        })
        return context

plugin_pool.register_plugin(CMSGalleryPlugin)

最后,迭代幻灯片图片的模板:

and finally, the template that iterates over the slide images:

{% for slide in gallery.slides.all %}
    <img src="{{ slide.image.url }}" alt="{{ slide.alt }}" />
{% endfor %}

这篇关于如何从Django模板中的页面遍历cms插件实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆