Django / WagtailCMS - 使用 get_context 获取 child 页面的属性(例如 body 文本)
Django / WagtailCMS - get attribute (eg body text) of child page using get_context
我正在尝试访问我的 Django / Wagtail CMS 博客中 child 页面的 body 文本。我可以 return child 页面标题,但我不知道如何使用它来获取 child 页面属性的其余部分。 parent是IndexPage,child是IndexListSubPage。我的模型是:
class IndexPage(Page):
body = RichTextField(blank=True)
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('feed_image'),
]
def get_context(self, request):
context = super(IndexPage, self).get_context(request)
context['sub_pages'] = self.get_children()
return context
class IndexListSubPage(Page):
body = RichTextField(blank=True)
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('feed_image'),
]
我在我的模板中尝试了各种组合:
{{ sub_pages }} //returns <QuerySet [<Page: Page title here>]>
{{ sub_pages.body }} //returns nothing
这 return 是 child 页面的页面标题,但我还需要其他属性,例如 body 文本。有任何想法吗?我也尝试了 中的图像模板设置 - 同样,我可以获得标题,但没有属性。该页面在管理界面中同时包含图像和 body 文本。
按照@gasman 的建议,我通过更改模型以包含 .specific()
使其正常工作。工作模型是:
class ProjectsPage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
]
def get_context(self, request):
context = super(ProjectsPage, self).get_context(request)
context['sub_pages'] = self.get_children().specific()
print(context['sub_pages'])
return context
并且在模板中:
{% with sub_pages as pages %}
{% for page in pages %}
{{ page.title }}
{{ page.body }}
{% endfor %}
{% endwith %}
现在正在呈现 child 页面的标题和 body。
我正在尝试访问我的 Django / Wagtail CMS 博客中 child 页面的 body 文本。我可以 return child 页面标题,但我不知道如何使用它来获取 child 页面属性的其余部分。 parent是IndexPage,child是IndexListSubPage。我的模型是:
class IndexPage(Page):
body = RichTextField(blank=True)
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('feed_image'),
]
def get_context(self, request):
context = super(IndexPage, self).get_context(request)
context['sub_pages'] = self.get_children()
return context
class IndexListSubPage(Page):
body = RichTextField(blank=True)
feed_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
ImageChooserPanel('feed_image'),
]
我在我的模板中尝试了各种组合:
{{ sub_pages }} //returns <QuerySet [<Page: Page title here>]>
{{ sub_pages.body }} //returns nothing
这 return 是 child 页面的页面标题,但我还需要其他属性,例如 body 文本。有任何想法吗?我也尝试了
按照@gasman 的建议,我通过更改模型以包含 .specific()
使其正常工作。工作模型是:
class ProjectsPage(Page):
body = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('body', classname="full"),
]
def get_context(self, request):
context = super(ProjectsPage, self).get_context(request)
context['sub_pages'] = self.get_children().specific()
print(context['sub_pages'])
return context
并且在模板中:
{% with sub_pages as pages %}
{% for page in pages %}
{{ page.title }}
{{ page.body }}
{% endfor %}
{% endwith %}
现在正在呈现 child 页面的标题和 body。