如何为从另一个页面的流场内容继承上下文的页面正确呈现模板?

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

这是结构块

class EventBlock(blocks.StructBlock):
    event_name = blocks.CharBlock(max_length=100, blank=False, help_text="Event Name")
    event_date = blocks.DateBlock(format="%B %d, %Y")
    start_time = blocks.TimeBlock(format="%H:%M")
    end_time = blocks.TimeBlock(format="%H:%M")
    brochure = ImageChooserBlock(required=False)
    address = blocks.CharBlock(max_length=200, blank=True, help_text="Address of Event")
    summary = blocks.RichTextBlock(max_length=5000)

这是活动页面内容

class EventPage(Page):
    objects = EventPageManager()
    start_time = models.DateTimeField()
    end_time = models.DateTimeField()

    content = StreamField(
        [
            ('event', blocks.EventBlock()),
        ], block_counts={
            "event": {"min_num": 1, "max_num": 1},
        }, use_json_field=True)

然后有一个索引页面从事件页面获取上下文

 class EventPageIndex(Page):
    max_count = 1
    subpage_types = ['_event.EventPage']

    # Featured Event(next upcoming event that is less than now but is closer to now)

    def get_context(self, request, *args, **kwargs):
        context = super().get_context(request, *args, **kwargs)

        context['future'] = EventPage.objects.future()
        context['past'] = EventPage.objects.past()
        return context

然后我放入索引页

<section class="past-events">
        <h2>Past Events</h2>
        {% for item in past %}
            <div>
                <h3>{{ item.title }}</h3>
            {% for item in past.content %}
                {% if item.block_type == "event" %}
                    {% for slide in item.value %}
                        {% if slide.block_type == "brochure" %}
                            {% image slide.value width-200 %}
                        {% endif %}
                    {% endfor %}
                {% endif %}
            {% endfor %}
            </div>
        {% endfor %}
    </section>

期待小册子出现在 event_listing_page.html 但是,我无法获得该信息以显示此特定代码。

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

当您在事件页面上只允许一种块和块的一个实例时,为什么要将其建模为 StreamField + StructBlock。如果您将其移动到 EventPage 中的实际字段,这将更容易处理。然后验证会更容易,你可以做像 EventPage.objects.fiter(start_date__gt=now)

这样的查询
© www.soinside.com 2019 - 2024. All rights reserved.