2

cms_plugin.py

@plugin_pool.register_plugin
class CarouselPlugin(CMSPluginBase):
    name = _("Carousel")
    model = CarouselPluginModel
    exclude = ['slides']
    inlines = [CarouselSlideInline]
    render_template = "plugins/touts/carousel.html"

    def render(self, context, instance, placeholder):
        context['instance'] = instance
        return context

模型.py

class CarouselPluginModel(CMSPlugin):

    slides = models.ManyToManyField('CarouselSlide', blank=True, related_name='slider_images')
    content = PlaceholderField('content')

    def slide_list(self):
        return CarouselSlide.objects.filter(carousel=self).order_by('order')

    def copy_relations(self, oldinstance):
        self.slides = oldinstance.slides.all()

插件/吹捧/carousel.html

{% load i18n staticfiles cms_tags menu_tags sekizai_tags %}

<div class="carousel">
  {% render_placeholder instance.content %}
  <div class="carousel__slider">
    {% for slide in instance.slide_list %}
      <div class="carousel__slider__slide" style="background-image: url('{{slide.image.url}}')">
          <div class="carousel__heading"><h1>{{slide.title}}</h1></div>
          <div class="carousel__sub-heading">
            <p>{{slide.sub_title}}</p>
          </div>
          {% if slide.button_link %}
            <a href="{{slide.button_link}}" class="carousel__button button-1">Learn More</a>
          {% endif %}
      </div>
    {% endfor %}
  </div>
</div>

我计划有两个不同的 CMS 插件,分别命名为“Carousel”和“Carousel Slide”。正如你可以从他们的名字中猜到的那样,我正在尝试将占位符放在 Carousel 插件中,这样我就可以将 Carousel Slides 放入其中。它看起来像 CMS 插件内的 CMS 插件。我怎样才能实现这种结构?

4

0 回答 0