您还没有共享您的模型定义,但我想应该是这样的:
class MyPage(Page):
images = StreamField([
('image_list', blocks.ListBlock(blocks.ImageChooserBlock)),
])
使用标准模式
manually looping over a StreamField value
如Wagtail文档所示,这将是:
{% for block in page.images %}
{% if block.block_type == 'image_list' %}
{# at this point block.value gives you the images as an ordinary Python list #}
{# Output the first image using block.value.0: #}
{% image block.value.0 width-800 %}
{# Or loop over block.value manually with a 'for' loop #}
<ul>
{% for img in block.value %}
<li>{% image img width-800 %}</li>
{% endfor %}
</ul>
{% elif block.block_type == 'some_other_block' %}
...
{% else %}
...
{% endif %}
{% endfor %}
在这种情况下,可能只定义了一个块类型(
image_list
所以
if block.block_type == 'image_list'
可以忽略不计,但你仍然需要外部的
{% for block in page.images %}
,因为streamfield仍然定义为块列表,即使该列表中只有一个项。