StreamField中的顶级StreamBlock不会在模板中渲染

2024-06-11 21:02:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用一个StreamBlock,就像docshere部分中的最后一个片段一样,作为StreamField的唯一参数。它在Wagtail管理中工作得很好,但是当尝试在模板中渲染时,什么都没有出现。我尝试使用默认模板,只包含_块,但没有渲染任何内容。我还尝试使用自定义模板和include_块,自定义模板被命中,但我无法从中呈现任何有用的内容。在下面的代码中,我恢复到尝试使用默认模板

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\u page.html:

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

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

我对python/django/wagtail相当陌生,所以希望这只是一个简单的概念性错误。感谢您的帮助。提前谢谢


Tags: thetextfromimageimportfalsehomeis
1条回答
网友
1楼 · 发布于 2024-06-11 21:02:10

在我的例子中,这不起作用,因为在管理中创建的块仍然处于草稿状态,没有发布(facepalm)。一旦出版,这本书就完美地发挥了作用

相关问题 更多 >