3

假设我正在关注Your first Wagtail site tutorial,除了我想使用 Streamfield,而不是单独的RichTextFieldBlogPageGalleryImage。例如 :

class BlogPage(Page):
    date = models.DateField("Post date")
    intro = models.CharField(max_length=250)
    body = RichTextField(blank=True)

……变成……

class BlogPage(Page):
    body = StreamField([
        ('heading', blocks.CharBlock(classname="full title")),
        ('intro', blocks.RichTextBlock()),
        ('body', blocks.RichTextBlock()),
        ('image', ImageChooserBlock()),
    ], null=True, blank=True,)

我如何(或我可以)从 Streamfield 中提取第一张图像和介绍的开头(...因为我不能限制长度)以用于 blog_index_page.html 模板,并避免使用单独的字段和BlogPageGalleryImage类?因此替换此模板:

{% for post in blogpages %}
{% with post=post.specific %}
    <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>

    {% with post.main_image as main_image %}
        {% if main_image %}{% image main_image fill-160x100 %}{% endif %}
    {% endwith %}

    <p>{{ post.intro }}<p>
{% endwith %}
{% endfor %}

有了更多这样的东西:

{% for post in blogpages %}
{% with post=post.specific %}
    <h2><a href="{% pageurl post %}">{{ post.title }}</a></h2>

    {{ post.body.image.first() }}

    <p>{{ post.intro.get_words(50) }}</p>
    {{ post.body|richtext }}
{% endwith %}
{% endfor %}

非常感谢。

4

0 回答 0