如何从Django模板中的页面迭代cms插件实例?

时间:2022-09-18 14:31:43

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

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

I thought of doing it like this:

我想这样做:

  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. 创建一个将在其中显示的模板。在这个模板中,做一些类似
      {% for plugin In page %}
    • plugin
    • {% endfor %}
    的事情。
  3. Make a youtube_videos placeholder and configure it to be limited to only that type of plugin.
  4. 创建一个youtube_video占位符,并将其配置为仅限于该类型的插件。

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.

但是我不知道如何在模板当前页面的插件实例上进行迭代。我在django-cms文档中没有看到任何关于这个的信息,是的,我猜django-cms“只是django”,如果我知道django是正确的,那么我应该已经知道了。

But a nice example here would be nice.

这里有个很好的例子。

1 个解决方案

#1


2  

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.

您不会在Django-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".

要做您想做的事情,您需要创建一个CMS插件,允许您创建一个模型的多个实例,您可以对其进行迭代,类似于“图像库”。

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

and a child model:

和一个孩子模型:

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

then you'd have a CMS Plugin like so:

然后你会有一个CMS插件像这样:

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 %}

#1


2  

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.

您不会在Django-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".

要做您想做的事情,您需要创建一个CMS插件,允许您创建一个模型的多个实例,您可以对其进行迭代,类似于“图像库”。

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

and a child model:

和一个孩子模型:

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

then you'd have a CMS Plugin like so:

然后你会有一个CMS插件像这样:

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 %}