我正在尝试创建一个索引页面,其中包含指向 Wagtail 中多个照片库的链接。GalleryIndexPage 模型如下所示:
class GalleryIndexPage(Page):
subpage_types = ['home.GalleryPage']
gallery_thumb = StreamField ([
('cover_photo', ImageChooserBlock()),
('title', blocks.CharBlock()),
('link', URLBlock()),
])
content_panels = Page.content_panels + [
StreamFieldPanel('gallery_thumb'),
]
我很难用围绕每组数据的“画廊项目”类将它渲染到模板中。我意识到它当前正在循环并为 Streamfield 内的每个块添加一个“画廊项目”类,而不是围绕整个 Streamfield 集。这是我的模板代码:
<div class="photo-gallery">
{% for block in self.gallery_thumb %}
<div class="gallery-item">
{% if block.block_type == 'cover_photo' %}
<div class="thumb">
{% image block.value fill-200x150 %}
</div>
{% endif %}
{% if block.block_type == 'title' %}
<div class="title">
<p>{{ block.value }}</p>
</div>
{% endif %}
{% if block.block_type == 'link' %}
<div class="link">
<a href="{{ block.value }}">View Gallery</a>
</div>
{% endif %}
</div>
{% endfor %}
我还有其他方法可以解决这个问题吗?
编辑:我在我的 StreamField 中添加了一个 StructBlock ,如下所示:
class GalleryIndexPage(Page):
subpage_types = ['home.GalleryPage']
gallery = StreamField ([
('gallery_item', blocks.StructBlock([
('cover_photo', ImageChooserBlock()),
('title', blocks.CharBlock()),
('link', URLBlock()),
], icon='user'))
])
content_panels = Page.content_panels + [
StreamFieldPanel('gallery'),
]
我不确定如何在我的模板中访问这些值?这是我到目前为止所拥有的:
<div class="photo-gallery">
{% for block in self.gallery %}
<div class="gallery-item">
<div class="thumb">
{% image self.cover_photo width-200 %}
</div>
<div class="title">
<p>{{ self.title }}</p>
</div>
<div class="link">
<a href="{{ self.link }}">>> View Gallery</a>
</div>
</div>
{% endfor %}
</div>