Django / Wagtail:尝试添加新的子级内容以显示在模板上

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

我有2个类别:CandlePageCandleIndexPage。定义CandlePage以描述蜡烛的详细信息。CandleIndexPage应显示蜡烛列表(CandlePage对象)。

我需要为每个CandleIndexPage对象添加一个'label'属性。我认为可以通过重写CandlePage方法来完成此操作,但是我无法在“儿童级别”执行此操作。很高兴获得任何建议。

models.py

get_context
django wagtail
1个回答
0
投票

只是想更新我通过以下方法解决的问题:1.将查询集转换为列表:

class CandleIndexPage(Page):
    intro = RichTextField(blank=True, null=True)
    image = models.ForeignKey(
        'wagtailimages.Image',on_delete=models.SET_NULL, null=True, related_name='+'
    )    
    content_panels = Page.content_panels + [
        FieldPanel('intro', classname="full"),
        ImageChooserPanel('image'),
        # InlinePanel('candle_post', label="Light a Candle"),
    ]

class CandlePage(Page):
    def get_context(self, request):
        context = super().get_context(request)
        context['candlelabel'] = str(self.name) + "foo bar" + str(self.date)
        return context

    name = models.CharField(blank=True, max_length=250)
    dedication = RichTextField(blank=True, null=True)
    date = models.DateField()
    image = models.ForeignKey(
        'wagtailimages.Image',on_delete=models.SET_NULL, null=True, related_name='+'
    )   
    content_panels = [
        # PageChooserPanel('candle_index_page','CandleIndexPage'),
        FieldPanel('name'),
        FieldPanel('date'),
        ImageChooserPanel('image'),
        FieldPanel('dedication'),

    ]
    def clean(self):
        super().clean()
        new_title = str(self.name) + str(self.date)
        self.title = new_title
        self.slug = slugify(new_title)
  1. 迭代蜡烛列表,并向列表中的每个蜡烛“行”添加一个“标签”属性

  2. 将更新的蜡烛列表作为属性添加到上下文对象并返回'context'

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