StreamField中的顶级StreamBlock不在模板中呈现

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

我正在尝试像文档here部分中的最后一个片段一样使用StreamBlock,作为StreamField的唯一参数。在Wagtail管理员中,它工作得很好,但是当尝试在模板中进行渲染时,什么也没有出现。我尝试使用默认模板,仅使用include_block,但未呈现任何内容。我也尝试过使用自定义模板和include_block,但自定义模板被选中,但是我无法从那里渲染任何有用的东西。我恢复为尝试在以下代码中使用默认模板。

home / 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'),
    ]

home / 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 / templates / home / home_page.html:

{% extends "base.html" %}
{% load wagtailcore_tags %}

{% block content %}
    test
    {% include_block page.hero_image %}
{% endblock content %}

我是python / django / wagtail的新手,所以希望它只是一个简单/概念性的错误。任何帮助表示赞赏。预先感谢。

python django django-templates wagtail wagtail-streamfield
1个回答
0
投票

在我的情况下,这是行不通的,因为在管理员中创建的块仍处于草稿状态,尚未发布(facepalm)。一旦发布,就可以完美运行。

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