如何显示 wagtail 页面的子页面
How to display the children of a wagtail page
我的要求听起来很简单,但我正在努力实现它
在我的 wagtail 项目中,我有一个 BlogListingPage,它有很多子页面。我正在尝试在网页中创建子项列表。这是我的代码:
models.py
class BlogListingPage(Page):
...
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['blog_pages'] = BlogDetailPage.objects.live().child_of(self)
return context
class BlogDetailPage(Page):
...
blog_listing = models.ForeignKey(BlogListingPage,
on_delete=models.PROTECT,
blank=True,
null=True,)
views.py
def blog(request):
url = 'blog/blog_listing_page.html'
context = {'data': BlogListingPage.objects.all()}
return render(request, url, context)
blog_listing_page.html
{% block content %}
{% for blog_listing in data %}
{% for post in blog_listing.blogdetailpage %}
<a href="{% pageurl post %}">{{ post.blog_title }}</a>
{% endfor %}
{% endfor %}
{% endblock content %}
我迷路了 django/wagtail pages/objects/querysets/all()/specific
谁能带我去碧水蓝天?
关于您的方法的一些建议:
拥有从 BlogDetailPage
到 BlogListingPage
的外键并不是组织 parent-child 关系的 Wagtail 方式。所有 Page
objects 已经有一个树层次结构,你应该依赖它,否则你将失去 Wagtail 提供的所有 built-in 树处理。
您应该使用 parent/subpage rules 来指定页面类型如何相互关联。在你的情况下,它看起来像:
class BlogListingPage(Page):
subpage_types = ['myapp.BlogDetailPage']
class BlogDetailPage(Page):
parent_page_types = ['myapp.BlogListingPage']
当 Wagtail 在视图中提供特定页面时,BlogListingPage
上的 get_context()
方法仅 运行s。当您在 blog
视图中遍历这些页面时,它不会 运行。要在那里访问 children,您需要执行以下操作:
{% for blog_listing in data %}
{% for post in blog_listing.get_children.live %}
<a href="{% pageurl post %}">{{ post.blog_title }}</a>
{% endfor %}
{% endfor %}
即需要在页面上使用get_children()
方法获取children。 live()
进一步过滤以排除未发布的页面。请注意,这仅在博客 post 是列表页面的 children(在 Wagtail 页面树意义上)时才有效。如果您只使用 post URL 和标题,specific()
在这里并不重要 - 如果您想显示属于模型一部分的字段而不是基础 Page
型号.
我的要求听起来很简单,但我正在努力实现它
在我的 wagtail 项目中,我有一个 BlogListingPage,它有很多子页面。我正在尝试在网页中创建子项列表。这是我的代码:
models.py
class BlogListingPage(Page):
...
def get_context(self, request, *args, **kwargs):
context = super().get_context(request, *args, **kwargs)
context['blog_pages'] = BlogDetailPage.objects.live().child_of(self)
return context
class BlogDetailPage(Page):
...
blog_listing = models.ForeignKey(BlogListingPage,
on_delete=models.PROTECT,
blank=True,
null=True,)
views.py
def blog(request):
url = 'blog/blog_listing_page.html'
context = {'data': BlogListingPage.objects.all()}
return render(request, url, context)
blog_listing_page.html
{% block content %}
{% for blog_listing in data %}
{% for post in blog_listing.blogdetailpage %}
<a href="{% pageurl post %}">{{ post.blog_title }}</a>
{% endfor %}
{% endfor %}
{% endblock content %}
我迷路了 django/wagtail pages/objects/querysets/all()/specific
谁能带我去碧水蓝天?
关于您的方法的一些建议:
拥有从
BlogDetailPage
到BlogListingPage
的外键并不是组织 parent-child 关系的 Wagtail 方式。所有Page
objects 已经有一个树层次结构,你应该依赖它,否则你将失去 Wagtail 提供的所有 built-in 树处理。您应该使用 parent/subpage rules 来指定页面类型如何相互关联。在你的情况下,它看起来像:
class BlogListingPage(Page): subpage_types = ['myapp.BlogDetailPage'] class BlogDetailPage(Page): parent_page_types = ['myapp.BlogListingPage']
当 Wagtail 在视图中提供特定页面时,
BlogListingPage
上的get_context()
方法仅 运行s。当您在blog
视图中遍历这些页面时,它不会 运行。要在那里访问 children,您需要执行以下操作:{% for blog_listing in data %} {% for post in blog_listing.get_children.live %} <a href="{% pageurl post %}">{{ post.blog_title }}</a> {% endfor %} {% endfor %}
即需要在页面上使用
get_children()
方法获取children。live()
进一步过滤以排除未发布的页面。请注意,这仅在博客 post 是列表页面的 children(在 Wagtail 页面树意义上)时才有效。如果您只使用 post URL 和标题,specific()
在这里并不重要 - 如果您想显示属于模型一部分的字段而不是基础Page
型号.