Python-Wagtail:在父页面内显示子页面列表
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Python-Wagtail:在父页面内显示子页面列表,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1674字,纯文字阅读大概需要3分钟。
内容图文
在Wagtail CMS中,我试图创建一个索引页面,该页面将显示其所有子页面的列表以及与每个子页面相关的特色图片.
我已经在models.py中创建了这两个页面模型:
class IndexPage(Page):
intro = RichTextField(blank=True)
content_panels = Page.content_panels + [
FieldPanel('intro', classname='full'),
]
subpage_types = ['myapp.ItemPage']
class ItemPage(Page):
representative_image = models.ForeignKey(
'wagtailimages.Image',
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name='+'
)
body = RichTextField(blank=True)
promote_panels = Page.promote_panels + [
ImageChooserPanel('representative_image'),
]
content_panels = Page.content_panels + [
FieldPanel('body', classname='full'),
]
在模板index_page.html中,添加了以下代码:
<div class="intro">{{ self.intro|richtext }}</div>
{% for page in self.get_children %}
{{ page.title }}
{% image page.representative_image width-400 %}
{% endfor %}
这将显示所有子页面标题,但不显示图像.是否可以检索子页面的图像字段?
解决方法:
从release notes of wagtail version 1.1:
Usually, an operation that retrieves a queryset of pages (such as
homepage.get_children()
) will return them as basic Page instances, which only include the core page data such as title. Thespecific()
method (e.g.homepage.get_children().specific()
) now allows them to be retrieved as their most specific type, using the minimum number of queries.
因此,在即将发布的1.1版中您不再需要自定义功能,并且可以将模板更改为:
{% for page in self.get_children.specific %}
{{ page.title }}
{% image page.representative_image width-400 %}
{% endfor %}
至少从版本0.8开始,以下内容也应该使用特定的方法工作:
{% for page in self.get_children %}
{{ page.title }}
{% image page.specific.representative_image width-400 %}
{% endfor %}
内容总结
以上是互联网集市为您收集整理的Python-Wagtail:在父页面内显示子页面列表全部内容,希望文章能够帮你解决Python-Wagtail:在父页面内显示子页面列表所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。