mat*_*usc 1 django django-templates wagtail
我在 Wagtail 中调用了 ListBlock images。效果很好。如果我把
{{ 页面.图片 }}
在模板中,它呈现如下 html 代码:
<ul>
<li>item1</li>
<li>item2</li>
</ul>
Run Code Online (Sandbox Code Playgroud)
但我无法找出如何隔离列表的第一项。或者至少如何手动迭代列表。
我很确定解决方案很简单,但是我无法通过谷歌搜索、在文档中找到或从 wagtail 源代码中理解。
您还没有分享您的模型定义,但我猜它是这样的:
class MyPage(Page):
images = StreamField([
('image_list', blocks.ListBlock(blocks.ImageChooserBlock)),
])
Run Code Online (Sandbox Code Playgroud)
使用Wagtail 文档中所示的手动循环 StreamField 值的标准模式,这将是:
{% 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 %}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,您可能只定义了一种块类型 ( image_list),因此if block.block_type == 'image_list'可以省略;但您仍然需要 external {% for block in page.images %},因为 StreamField 仍然定义为块列表,即使该列表中只有一项。
| 归档时间: |
|
| 查看次数: |
1981 次 |
| 最近记录: |