g:获取上一个或下一个同级 [英] Wagtail: Get previous or next sibling
问题描述
我正在创建一个带有ag的页面,在这里我需要知道当前页面的上一个和下一个兄弟姐妹:
I'm creating a page with wagtail where I need to know the previous and next sibling of the current page:
在我的肖像页面模型中,我试图定义两个方法来找到正确的网址,但是我缺少一个关键部分。要获得第一个兄弟姐妹,我可以执行以下操作:
In my portrait page model, I tried to define two methods to find the correct urls, but I'm missing a crucial part. To get the first sibling, I can just do the following:
class PortraitPage(Page):
...
def first_portrait(self):
return self.get_siblings().live().first().url
有 first()
和 last()
方法,但似乎没有 next()
或 previous()
方法直接邻居(按照他们在ag管理中的排列顺序)。
There is the first()
and last()
method, but there doesn't seem to be a next()
or previous()
method to get the direct neighbours (in the order that they are arranged in the wagtail admin).
有什么方法可以实现这一目标?
Is there any way to achieve this?
推荐答案
经过一段时间的调试器后,我发现wagtail已经有两种方法: get_prev_sibling()
和 get_next_sibling()
。
After going through the debugger for a while, I found out that wagtail already has two methods: get_prev_sibling()
and get_next_sibling()
.
因此这些方法看起来像这样(说明上一个方法的第一页以及下一个方法的最后一项):
So the methods could look like this (accounting for the first page in the previous method and the last item in the next method):
def prev_portrait(self):
if self.get_prev_sibling():
return self.get_prev_sibling().url
else:
return self.get_siblings().last().url
def next_portrait(self):
if self.get_next_sibling():
return self.get_next_sibling().url
else:
return self.get_siblings().first().url
这篇关于g:获取上一个或下一个同级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!