我正在尝试使用像此处文档部分中的最后一个片段一样的 StreamBlock ,作为 StreamField 的唯一参数。它在 Wagtail 管理员中运行良好,但是当尝试在模板中渲染时,什么也没有出现。我尝试使用默认模板并仅使用 include_block,但没有呈现任何内容。我还尝试使用自定义模板和 include_block,并且自定义模板被击中,但我无法从那里渲染任何有用的东西。我恢复尝试在以下代码中使用默认模板。
主页/models.py:
from django.db import models
from wagtail.core.models import Page
from wagtail.core.fields import StreamField
from wagtail.admin.edit_handlers import StreamFieldPanel
from home.blocks import HeroImageStreamBlock
class HomePage(Page):
hero_image = StreamField(HeroImageStreamBlock(
block_counts={
'hero_button': {'max_num': 1},
'is_active': {'max_num': 1},
'is_randomized': {'max_num': 1},
}
)
, blank=True
)
content_panels = Page.content_panels + [
StreamFieldPanel('hero_image'),
]
主页/blocks.py:
from wagtail.core import blocks
from wagtail.images.blocks import ImageChooserBlock
class HeroButtonBlock(blocks.StructBlock):
button_text = blocks.CharBlock(required=False)
button_page = blocks.PageChooserBlock(required=False)
button_url = blocks.URLBlock(required=False)
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero button is active.
NOTE: Only the first active hero button will be displayed.""")
class Meta:
icon = "link"
class HeroTextBlock(blocks.StructBlock):
hero_text = blocks.CharBlock(required=False)
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero text is active.
NOTE: Only the first active hero text will be displayed.""")
class Meta:
icon = "edit"
class HeroImageBlock(blocks.StructBlock):
"""This is the StructBlock for the hero images."""
background_image = ImageChooserBlock()
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero image is active.""")
class Meta:
icon = "image"
class HeroImagesListBlock(blocks.ListBlock):
class Meta:
icon = "media"
class HeroImageStreamBlock(blocks.StreamBlock):
"""This is the container StreamBlock for the hero image."""
hero_images = HeroImagesListBlock(HeroImageBlock())
text = HeroTextBlock()
hero_button = HeroButtonBlock()
is_randomized = blocks.BooleanBlock(required=False,
label="Is Randomized?",
help_text="When checked, images will load in random order.")
is_active = blocks.BooleanBlock(required=False,
help_text="""When checked, this hero image is active.
NOTE: Only the first active hero image will be displayed.""")
主页/模板/主页/home_page.html:
{% extends "base.html" %}
{% load wagtailcore_tags %}
{% block content %}
test
{% include_block page.hero_image %}
{% endblock content %}
我对 python/django/wagtail 很陌生,所以希望这只是一个简单/概念性的错误。任何帮助表示赞赏。提前致谢。