0

我尝试在页面中呈现子页面的 StreamField。我无法在 StreamField 中呈现不同的 StructField。这是我的代码

class DefinitionPage(Page):

body = StreamField([
    ('definition', blocks.StructBlock([
        ('heading', blocks.CharBlock(label='Titre')),
        ('paragraph', blocks.RichTextBlock(label='Paragraphe')),
    ]))
])

content_panels = Page.content_panels + [
    StreamFieldPanel('body'),
]

我的模板。(DefinitionPage 是此页面的子页面。)

{% for post in page.get_children %}
    <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>
    {% for block in post.body %}
        {% include_block block %}
    {% endfor %}
{% endfor %}

post.title 没问题,但就像 post.body 中没有块一样。我尝试了很多东西,{% include_block block %} 肯定是错误的。我还尝试为 StructBlock 添加自定义模板,但没有成功。

我能怎么做 ?我正在使用 Django 2.0 和 wagtail 2.0(我是 wagtail 的新手,但我阅读了文档)最好的问候

4

2 回答 2

0

您需要使用page.get_children.specific-get_children只返回所有页面类型共有的基本信息,Page不包括body.DefinitionPage

于 2018-02-25T13:30:52.537 回答
0

谢谢我更改了我的代码在我添加的父 PageModel 中:

def get_context(self, request):
    context = super().get_context(request)
    definitions = self.get_children().specific()
    context['definitions'] = definitions
    return context

现在在我的模板中:

{% for definition in definitions %}
    {% for block in definition.body %}
        {%  include_block block %}
    {% endfor %}
{% endfor %}

我还为我的定义创建了一个自定义模板(很简单,它只是在测试中):

{% load wagtailcore_tags %}

<div class="definition">
    <h2>{{ value.bound_blocks.heading }}</h2>
    {{ value.bound_blocks.paragraph }}
</div>

非常感谢

最后一个问题:有更好的做法吗?.specific 在模板或自定义 get_context() 中?将自定义模板添加到 StructBlock 是一种好习惯吗?

于 2018-02-25T15:12:00.210 回答