以类似片段的方式重新使用 wagtail 页面字段

问题描述 投票:0回答:1

这就是我想要做的:创建一个图像/徽标的水平滚动,在大多数网站页面的屏幕底部滚动 - 选框、纸条之类的东西..我对 CSS/ 没意见html 我可以正常工作,问题是我不想将图像/徽标硬编码到 html 中。

由于这并不是一个真正的页面,我认为这样做的最佳原因是使用一个片段并使用标准 django 模型设置选取框的内容。唯一的问题是,该网站是针对职业运动员的,并且徽标是赞助商徽标 - 因此每个赞助商已经有一个鹡鸰页面,并且有赞助商页面的地方也有赞助商的徽标。徽标已保存在数据库中,因此似乎没有必要重新实现代码来再次加载和检索每个徽标。

有没有办法通过片段使用现有页面中的项目(在本例中为图像/徽标)?作为一个新手,我可以想象有一种更好的方法可以在不使用片段的情况下做到这一点,但我不知道如何做,任何人都可以帮忙吗?

这是赞助商(或合作伙伴)索引和页面的代码

class PartnerIndexPage(Page):
    intro = StreamField(BaseStreamBlock(),
                        verbose_name="Introduction",
                        blank=True,
                        use_json_field=True, )

    def get_context(self, request, *args, **kwargs):
        # restrict context to live, public and effective_end_date > today

        context = super().get_context(request)
        today = date.today()
        partnerpages = PartnerPage.objects.child_of(self).live().public().filter(effective_end_date__gte=today)
        context['partnerpages'] = partnerpages
        return context

    subpage_types = ['PartnerPage']
    parent_page_types = ['home.HomePage']

    content_panels = Page.content_panels + [
        FieldPanel('intro', classname="full")
    ]


class PartnerPage(Page):
    class LogoOrientation(models.IntegerChoices):
        L = 1, _('Landscape')
        P = 2, _('Portrait')
        S = 3, _('Square or Circular')

    name = models.CharField(blank=False, max_length=64,
                            help_text="E.g. IBM, Toyota, Ferrari, etc.")
    effective_end_date = models.DateField("End of partnership date", null=True,
                                          help_text="Set the date when partner's details will no longer be "
                                                    "visible on the site")
    tag_line = models.CharField(blank=True, null=True, max_length=64,
                                help_text="This isn't tags like in the news but more like 'Vorsprung durch Technik'")
    body = StreamField(BaseStreamBlock(),
                       verbose_name="Page body",
                       help_text="Partner information",
                       blank=True,
                       use_json_field=True, )

    phone = models.CharField(blank=True, max_length=32)
    email = models.CharField(blank=True, max_length=64)
    web = models.URLField(blank=True, help_text=_("Homepage URL"))
    facebook = models.URLField(blank=True, help_text=_("Facebook URL"))
    twitter = models.URLField(blank=True, help_text=_("Twitter URL"))
    youtube = models.URLField(blank=True, help_text=_("YouTube URL"))
    instagram = models.URLField(blank=True, help_text=_("Instagram URL"))
    google = models.URLField(blank=True, help_text=_("Google URL"))
    pinterest = models.URLField(blank=True, help_text=_("Pinterest URL"))

    logo_orientation = models.IntegerField(choices=LogoOrientation.choices, default=LogoOrientation.L)

    def main_image(self):
        gallery_item = self.partner_logo.first()
        if gallery_item:
            return gallery_item.image
        else:
            return None

    # search_fields = Page.search_fields + [
    # index.SearchField('name'),
    # index.SearchField('role'),
    # ]

    content_panels = Page.content_panels + [
        MultiFieldPanel([
            FieldPanel('effective_end_date'),
        ], heading="Meta Information"),
        MultiFieldPanel([
            FieldPanel('name'),
            FieldPanel('tag_line'),
            FieldPanel('body'),
        ], heading="Partner Display Information"),
        MultiFieldPanel([
            FieldPanel('phone'),
            FieldPanel('email'),
            FieldPanel("web"),
            FieldPanel("facebook"),
            FieldPanel("twitter"),
            FieldPanel("youtube"),
            FieldPanel("instagram"),
            FieldPanel("google"),
            FieldPanel("pinterest"),
        ], heading="Contact information, website and social media"),
        MultiFieldPanel([
            #FieldPanel("logo_orientation"),
            InlinePanel('partner_logo', label="Partner logo"),
        ], heading="Logo Information"),
    ]

    parent_page_types = ['PartnerIndexPage']
    subpage_types = []

    class Meta:
        verbose_name = "partner page"


class PartnerPageGalleryImage(Orderable):
    page = ParentalKey(PartnerPage, on_delete=models.CASCADE, related_name='partner_logo')
    image = models.ForeignKey(
        'wagtailimages.Image', on_delete=models.CASCADE, related_name='+'
    )
    caption = models.CharField(blank=True, max_length=256)

    panels = [
        FieldPanel('image'),
        FieldPanel('caption'),
    ]
wagtail wagtail-snippet
1个回答
0
投票

我可能会将合作伙伴设置为片段,或者通过外键关系字段将合作伙伴页面链接到合作伙伴模型并添加其他页面数据,或者在片段中包含所有内容(包括流字段)并使用可路由页面提供合作伙伴页面和索引。

通过您的设置,您可以将选取框创建为代码片段,并使用外键访问合作伙伴页面并从中提取信息。我认为可以将 PageChooser 限制为某些页面类型。不过,将您的合作伙伴信息迁移到片段模型中将是更简单的路线。

© www.soinside.com 2019 - 2024. All rights reserved.